2 ways to use PHP to determine the running status of a program in Linux_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:45
Original
736 people have browsed it

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? I wrote two methods below:

The first one: Use regular matching in Linux

Copy the code The code is as follows:

function ifrun($clsname ,$bf = 0)
{
//Detect below, if there is a process running, it will not run
$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&g t;=2)
{
return 1;
}
else
{
return 0;
}
}
}

Call:

Copy code The code is as follows:
if (ifrun('pooy',5)) { die("pooy is running "); }

Note: pooy is the name of the program pooy.php!

The second method: write the process into a file, then use the file function to read and match the string


Copy the code The code is as follows:
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_______________________________________________";
}

Note: "www/pooy" is a string contained in the program!

Is the PHP program running much more smoothly now on Linux?

http://www.bkjia.com/PHPjc/764615.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764615.htmlTechArticleSometimes 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...
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