有些时候需要些 php 定时脚本,但是必须保证服务器中每次只有一个该进程在执行中,所以可以采用如下方法来判断上一个进程是否在执行中
/** * @Purpose : 判断该进程是否在执行中,保证每一次只有一个 test.php 进程在进行 * @Method Name : whetherPorcessIsExecuting() * @parameter : (无) * @return : (无) */ private function whetherProcessIsExecuting() { //判断该进程是否在执行中 @exec("ps aux | grep test.php",$result); // 找出与进程“test.php”有关的信息,并将结果保存在 $result 中 $sum = count($result); if($sum > 3){ // 保证每次只有一个 test.php 进程在执行 echo "上一个进程还在执行中!"; exit(); } /* var_dump($result_test); array(3) { [0]=> string(123) "root 26483 0.0 0.9 196384 9412 pts/2 S+ 16:38 0:00 php /data/myproject/auto/test.php" [1]=> string(107) "root 26484 0.0 0.1 63888 1044 pts/2 R+ 16:38 0:00 sh -c ps aux | grep test.php" [2]=> string(107) "root 26486 0.0 0.0 63888 160 pts/2 R+ 16:38 0:00 sh -c ps aux | grep test.php" }*/ }
PS AUX 查看系统所有进程数据
| 是管道命令
管道命令仅会处理 standard output , 对 standard error output 则会予以忽略
管道命令必须要能够接收来自前一个命令的数据成为 standard input 继续处理才行
grep 是选取命令
php exec($command, $output , $return_var)函数, ---------- 执行一个外部函数
commond : 执行的命令
output : 执行的输出填充此数组,每行输出填充数组的一个元素
return_var : 执行后的返回状态
本文为工作中所作的总结,原创作品,如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/7085293.html