Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP判断程序运行状态步骤详解

PHP判断程序运行状态步骤详解

php中世界最好的语言
php中世界最好的语言asal
2018-05-21 11:11:382482semak imbas

这次给大家带来PHP判断程序运行状态步骤详解,PHP判断程序运行状态的注意事项有哪些,下面就是实战案例,一起来看一下。

在linux系统中,运行一些脚本时,经常要放到crontab里面定时运行。
时间长了就有一个问题,那就是程序重复运行消耗太多的资源,怎么处理呢?

写了两种方法:
第一种:用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],&#39;www/pooy&#39;) !== FALSE) {
  //echo &#39;"earth" not found in string&#39;;
      $count[]=&#39;no&#39;;
      break;
  }
}
if(count($count) >= 1 )
{
    echo "A same programs are running";
    exit();
}else
{
    echo "start";
}

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

现在php程序在linux运行就会通畅多了。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

array_combine()数组合并函数使用案例详解

array_search()函数按元素值返回键名步骤详解

Atas ialah kandungan terperinci PHP判断程序运行状态步骤详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn