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...