Php judges linux program problems
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. Address: http://www.pooy.net/php-linux-grep.html
- // Pooy blog
- // Www.pooy.net
- // Welcome to the discussion!
- // Sometimes, when writing scripts on the server, it is often necessary to put them in crontab for regular operation. After a long time, there is a problem, that is, the program repeatedly consumes too much resources. how can this problem be solved? The following two methods are provided:
- // Method 1: use the regular expression matching in linux
- 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:
- 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?
- // Http://www.pooy.net/php-linux-grep.html
|