Sometimes when you write some scripts on the server, you often have to put them in crontab and run them regularly. Long time, there is a problem, that is, the program repeatedly run consumes too much resources, how to deal with it? Here are two ways I wrote:
The first: using regular matches in Linux
Copy the Code code as follows:
function Ifrun ($clsname, $BF = 0)
{
The following detects that if a process is running, it does 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 >=2)
{
return 1;
}
Else
{
return 0;
}
}
}
Call:
Copy the Code code as follows:
if (Ifrun (' Pooy ', 5)) {Die ("Pooy is Running");}
Note: Pooy is the name of the program pooy.php!
The second type: write the process to the file, then use the file function to read and then match the string
Copy the Code code 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 is running";
Exit ();
}else
{
echo "start__________________________________________________";
}
Note: "Www/pooy" is a string contained within the program!
is the PHP program running much smoother on Linux now?
http://www.bkjia.com/PHPjc/764615.html www.bkjia.com true http://www.bkjia.com/PHPjc/764615.html techarticle Sometimes when you write some scripts on the server, you often have to put them in crontab and run them regularly. Long time there is a problem, that is, the program repeatedly run consumes too much resources, how ...