How does PHP determine the program running status?

Source: Internet
Author: User
How does PHP determine the program running status?

In linux, when running some scripts, it is often necessary to regularly run them in crontab. 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?

Two methods are written: the first method is to match the regular expression in linux.

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

  2. {
  3. // Perform the following Check. if a process is running, it does 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. }

Call: if (ifrun ('poo', 5) {die ("pooy is running");} remarks: 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.

  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. }

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

Now the php program runs smoothly in linux.

Summary: crontab is a good option for debugging some simple php programs in linux systems, especially php programs that require scheduled execution. it is similar to the scheduled tasks in windows, however, it is more convenient than win. we suggest you try more.

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.