Php judges linux program problems-php Tutorial

Source: Internet
Author: User
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

  1. // Pooy blog
  2. // Www.pooy.net
  3. // Welcome to the discussion!
  4. // 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:
  5. // Method 1: use the regular expression matching in linux
  6. Function ifrun ($ clsname, $ bf = 0)
  7. {
  8. // Perform the following Check. if a process is running, it does not run.
  9. $ Str = shell_exec ("/bin/ps ax>/home/root/". $ clsname. "_run.txt ");
  10. $ Str = shell_exec ("/bin/grep-c '". $ clsname. ". php'/home/root/". $ clsname. "_run.txt ");
  11. If ($ bf> 0)
  12. {
  13. If ($ str >=$ bf)
  14. {
  15. Return 1;
  16. }
  17. Else
  18. {
  19. Return 0;
  20. }
  21. }
  22. Else
  23. {
  24. If ($ str> = 2)
  25. {
  26. Return 1;
  27. }
  28. Else
  29. {
  30. Return 0;
  31. }
  32. }
  33. }
  34. // Call:
  35. If (ifrun ('poo', 5 ))
  36. {
  37. Die ("pooy is running ");
  38. }
  39. // Note: pooy is the name of the program pooy. php!
  40. // Type 2: write the process to the file, use the file function to read and match the string.
  41. System ('PS-ef | grep wget>/root/pooy.txt ');
  42. $ Arr = file ('/root/pooy.txt ');
  43. $ Total = count ($ arr );
  44. For ($ I = 0; $ I <$ total; $ I ++ ){
  45. $ Count = array ();
  46. If (stristr ($ arr [$ I], 'www/pooy ')! = FALSE ){
  47. // Echo '"earth" not found in string ';
  48. $ Count [] = 'no ';
  49. Break;
  50. }
  51. }
  52. If (count ($ count)> = 1)
  53. {
  54. Echo "A same programs are running ";
  55. Exit ();
  56. } Else
  57. {
  58. Echo "start __________________________________________________";
  59. }
  60. // Note: "www/pooy" is a string contained in the program!
  61. // Is the php program running more smoothly in linux?
  62. // Http://www.pooy.net/php-linux-grep.html

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.