Sometimes, when writing some scripts on the server, it is often necessary to put them in the crontab for regular operation. After a long time, there is a problem, that is, repeated running of the program consumes too much resources. How can this problem be solved? I have written two methods below:
First: use the regular expression matching in linux
Copy codeThe Code is as follows:
Function ifrun ($ clsname, $ bf = 0)
{
// Perform the following check. 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 codeThe Code is as follows:
If (ifrun ('poo', 5) {die ("pooy is running ");}
Note: pooy is the name of the program pooy. php!
Type 2: Write the process into the file, use the file function to read and then match the string.
Copy codeThe 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 more smoothly in linux?