Linux中用PHP判断程序运行状态的2个方法

编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
这篇文章主要介绍了Linux中用PHP判断程序运行状态的2个方法,需要的朋友可以参考下

有时候在服务器上面写一些脚本的时候,经常要放到crontab里面定时运行。时间长了就有一个问题,那就是程序重复运行消耗太多的资源,怎么处理呢?狼蚁网站SEO优化我写了两种方法:

第一种:用linux里面的正则匹配

代码如下:

function ifrun($clsname,$bf = 0)
{
    //狼蚁网站SEO优化进行检测,如有一个进程正在运行,则不运行
    $str=shell_exec("/bin/ps ax > /home/root/".$clsname."_run.txt");
    $str=shell_exec("/bin/grep -c '".$clsname.".php' /home/root/".$clsname."_run.txt");

    if($bf >0)
    {
        if($str >=$bf)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        if ($str>=2)
        {
           return 1;
        }
        else
        {
           return 0;
        }
    }
}

调用:

代码如下:

if (ifrun('pooy',5)) {    die("pooy is running"); }

备注:pooy是程序pooy.php的名称!

第二种:把进程写到文件里面,然后用file函数去读取然后去匹配字符串

代码如下:

system('ps -ef |grep wget > /root/pooy.txt');
$arr=file('/root/pooy.txt');
$total=count($arr);
for($i=0;$i<$total;$i++){
  $count=array();
   if(stristr($arr[$i],'www/pooy') !== FALSE) {
    //echo '"earth" not found in string';
      $count[]='no';
      break;
  }

}

if(count($count) >= 1 )
{
    echo "A same programs are running";
    exit();
}else
{
    echo "start__________________________________________________";
}

注:”www/pooy” 是程序里面包含的字符串!


现在php程序在linux运行是否通畅多了呢?

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板