Two methods for determining the program running status using PHP in Linux

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.