返回 php判断li...... 登陆

php判断linux下程序问题实例

巴扎黑 2017-01-21 11:02:09 285

本文实例讲述了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],'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运行是否通畅多了呢?

更多关于php判断linux下程序问题实例请关注PHP中文网(www.php.cn)其他文章! 

最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网