This time to bring you the PHP Judgment program running state steps in detail, PHP judgment program running state of attention to what matters, the following is the actual case, together to see.
In a Linux system, when running some scripts, it is often necessary to run them in crontab.
Long time, there is a problem, that is, the program repeatedly run consumes too much resources, how to deal with it?
There are two ways of writing:
The first: Match Linux in the same way
function Ifrun ($clsname, $BF = 0) { //is detected below, and if a process is running, $str =shell_exec ("/bin/ps ax >/home/root/" is not run.) 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 (' 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
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!
Now the PHP program will run much smoother on Linux.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Array_combine () array merge function use case study
array_search () function returns the key name by element value step by steps