判断一个进程是否在执行中

有些时候需要些 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

转载于:https://www.cnblogs.com/chrdai/p/7085293.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值