怎么在Linux中使用PHP判断程序的运行状态

怎么在Linux中使用PHP判断程序的运行状态?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

复制代码 代码如下:


function ifrun($clsname,$bf = 0)
{
    //下面进行检测,如有一个进程正在运行,则不运行
    $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__________________________________________________";
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对亿速云的支持。