PHP scheduled task method to verify whether multiple processes call the same job_php skills

WBOY
Release: 2016-05-16 20:03:47
Original
956 people have browsed it

The example in this article describes the method of verifying whether multiple processes call the same job in PHP scheduled tasks. Share it with everyone for your reference, the details are as follows:

When using scheduled tasks, the company once had a situation where two processes ran the same scheduled task, resulting in many jobs being executed twice. In order to prevent this situation, a limit must be placed on the Linux process. The same If there is a process calling this scheduled task, then another process will not be allowed to call it again. The following is the specific code.

// $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') . "_] 进程正在执行中
\n";
    exit();
   }
  }
}

Copy after login

I hope this article will be helpful to everyone in php programming.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template