#检测是否是唯一运行
if(!checkSingleRun($_SERVER['SCRIPT_FILENAME']))
{
	echo "{$_SERVER['SCRIPT_FILENAME']} is runnging....\n";
	exit;
}

#检测是否存在相同的进程正在运行,仅支持在linux下运行,windows下无法检测
function checkSingleRun($cronName,$limit=1,$isDebug=false)
{
	$flagIsRunning = true;
	$osIsLinux = strtoupper(substr(PHP_OS, 0, 3)) != 'WIN';
	if($osIsLinux)
	{
		$arrayOutput = array();
		exec('ps -fea | grep ' . $cronName . ' | grep -v grep', $arrayOutput);
		$counter = 0;
		#调试模式时打印出匹配的进程信息
		if($isDebug)
			print_r($arrayOutput);

		$cronName_preg = str_replace('/','\/',$cronName);
		foreach ($arrayOutput as $value) 
		{
			if (preg_match('/^.*\s' . $cronName_preg . '.*$/', $value) 
					|| preg_match('/^.*\s.+(?:\x2F|\x5C)' . $cronName_preg . '.*$/', $value) ) 
			{
				$counter++;
			}
		}
		if ($counter > $limit) {
			$flagIsRunning = false;
		}
	}
	return $flagIsRunning;
}


声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权