PHP to determine Linux program problems

Source: Internet
Author: User

Sometimes when you write some scripts on the server, you often have to put them in crontab and run them regularly. 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 to write the following jade.
Original address: http://www.pooy.net/php-linux-grep.html

  1. Jade (Pooy) Blog
  2. Www.pooy.net
  3. Welcome to exchange, discussion!
  4. Sometimes when you write some scripts on the server, you often have to put them in crontab and run them regularly. Long time has a problem, that is, the program is repeated transport//line consumes too much resources, how to deal with it? Here are two ways to write the Jade:
  5. The first: using regular matches in Linux
  6. function Ifrun ($clsname, $BF = 0)
  7. {
  8. The following detects that 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 (' Pooy ', 5))
  36. {
  37. Die ("Pooy is running");
  38. }
  39. Note: Pooy is the name of the program pooy.php!
  40. The second type: write the process to the file, then use the file function to read and then 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 is running";
  55. Exit ();
  56. }else
  57. {
  58. echo "start__________________________________________________";
  59. }
  60. Note: "Www/pooy" is a string contained within the program!
  61. is the PHP program running much smoother on Linux now?
  62. Download Address: http://www.pooy.net/php-linux-grep.html
Copy Code
  • 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.