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(); } } }
I hope this article will be helpful to everyone in php programming.