PHP determines program problem examples under Linux, PHP determines Linux examples_PHP tutorial

WBOY
Release: 2016-07-13 09:46:50
Original
918 people have browsed it

An example of php judging program problems under linux, php judging linux example

The example in this article tells about php judging program problems under linux. Share it with everyone for your reference. The details are as follows:

Sometimes when writing some scripts on the server, they often need to be put into crontab to run regularly. After a long time, there will be a problem, that is, the repeated running of the program consumes too many resources. How to deal with it? Puyu writes two methods below.

//第一种:用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运行是否通畅多了呢?
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1029590.htmlTechArticleExamples of php judging program problems under linux, php judging linux examples This article tells the example of php judging program problems under linux. Share it with everyone for your reference. The details are as follows: Sometimes on the server...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template