Home  >  Article  >  Backend Development  >  php计划任务之验证是否有多个进程调用同一个job的方法_PHP

php计划任务之验证是否有多个进程调用同一个job的方法_PHP

WBOY
WBOYOriginal
2016-05-28 13:13:01774browse

本文实例讲述了php计划任务之验证是否有多个进程调用同一个job的方法。分享给大家供大家参考,具体如下:

在使用计划任务的时候,公司有一次出现过2个进程跑同一个计划任务的情况,导致很多job都执行了2次,为了预防这种情况需要对linux的进程做一个限制,同一时间如果有进程在调用这个计划任务,那么就不允许另一个进程再进行调用了,下面是具体的代码。

// $pro 方法名字 
private function _verifyPsAux($pro)
{
  $arrProcess = array(
   $pro => "/usr/local/www/scrm/public/index.php /records/job/{$pro}"
  );
  $pidNumber = 0;
  foreach ($arrProcess as $key => $value) {
   exec("ps aux|grep '$key'", $return);
   $isRunning = false; // 指令未执行
   foreach ($return as $k => $v) {
    if(! strrpos($v, $value)) continue;
    // preg_match('/\d+:\d+ +\/usr/iu', $v, $match);
    // if (! isset($match[0])) continue;
    $isRunning = true;
    $pidNumber++;
   }
   // 如果当前进程存在,则终止
   if ($isRunning && $pidNumber > 1) {
    echo '[_' . date('Y-m-d H:i:s') . "_] 进程正在执行中\r\n";
    exit();
   }
  }
}

希望本文所述对大家php程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn