强制PHP命令行脚本单进程运行的方法
发布时间:2020-12-12 20:08:55 所属栏目:PHP教程 来源:网络整理
导读:代码如下: /** * 保证单进程 * * @param string $processName 进程名 * @param string $pidFile 进程文件路径 * @return boolean 是否继续执行当前进程 */ function singleProcess($processName,$pidFile) { if (file_exists($pidFile) $fp = @fopen($pidFil
代码如下: /**
* 保证单进程 * * @param string $processName 进程名 * @param string $pidFile 进程文件路径 * @return boolean 是否继续执行当前进程 */ function singleProcess($processName,$pidFile) { if (file_exists($pidFile) && $fp = @fopen($pidFile,"rb")) { flock($fp,LOCK_SH); $last_pid = fread($fp,filesize($pidFile)); fclose($fp); if (!empty($last_pid)) if ($command == $processName) $cur_pid = posix_getpid(); if ($fp = @fopen($pidFile,"wb")) return true; /** return $command; 使用方法: 代码如下: if (singleProcess(getCurrentCommand(),'path/to/script.pid')) { // code goes here } else { exit("Sorry,this script file has already been running ...n"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |