Php judges program problem instances in linux, and php judges linux instances.
This article describes how php judges programs in linux. Share it with you for your reference. The details are as follows:
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? The following two methods are provided.
// Method 1: use the regular expression matching function ifrun ($ clsname, $ bf = 0) in linux {//. If a process is running, 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: if (ifrun ('poo', 5 )) {die ("pooy is running");} // note: Pooy is the name of the program pooy. php! // Type 2: Write the process to the file, use the file function to read and match the string 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?
I hope this article will help you with php programming.