Home  >  Article  >  Backend Development  >  How to determine program running status in PHP

How to determine program running status in PHP

WBOY
WBOYOriginal
2016-07-25 09:13:111657browse

In Linux systems, when running some scripts, they often need to be placed in crontab to run regularly. After a long time, there will be a problem, that is, the repeated running of the program consumes too many resources. How to deal with it?

Written two methods: The first one: Use regular matching in Linux

  1. function ifrun($clsname,$bf = 0)

  2. {
  3. //Detect below, if there is a process running, it will not run
  4. $str=shell_exec("/bin /ps ax > /home/root/".$clsname."_run.txt");
  5. $str=shell_exec("/bin/grep -c '".$clsname.".php' /home/root/ ".$clsname."_run.txt");

  6. if($bf >0)

  7. {
  8. if($str >=$bf)
  9. {
  10. return 1;
  11. }
  12. else
  13. {
  14. return 0;
  15. }
  16. }
  17. else
  18. {
  19. if ($str>=2)
  20. {
  21. return 1;
  22. }
  23. else
  24. {
  25. return 0;
  26. }
  27. }
  28. }
  29. < ;/p>
Copy code

Call: if (ifrun('pooy',5)) { die("pooy is running"); } Note: pooy is the name of the program pooy.php!

The second method: write the process into the file, then use the file function to read and match the string

  1. system('ps -ef |grep wget > /root/pooy.txt');

  2. $arr=file('/root/pooy.txt');
  3. $total =count($arr);
  4. for($i=0;$i<$total;$i++){
  5. $count=array();
  6. if(stristr($arr[$i],'www/pooy' ) !== FALSE) {
  7. //echo '"earth" not found in string';
  8. $count[]='no';
  9. break;
  10. }
  11. }

  12. if( count($count) >= 1 )

  13. {
  14. echo "A same programs are running";
  15. exit();
  16. }else
  17. {
  18. echo "start________________________________________________";
  19. }

Copy Code

Note: "www/pooy" is the string contained in the program!

Now the PHP program will run much more smoothly on Linux.

Editor’s summary: When debugging some simple PHP programs in a Linux system, especially PHP programs that need to be executed regularly, crontab is a good choice. It is similar to the scheduled tasks under Windows, but it is more convenient than under Win. It is recommended that you try it more.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn