Php judges program problem instances in linux

Source: Internet
Author: User
This article mainly introduces php's determination of program problems in linux, which can effectively control resource allocation during scheduled execution of crontab control programs in Linux, which is of great practical value, for more information about how to use php to identify program problems in linux, see the example in this article. Share it with you for your reference. The details are as follows:

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.

// Method 1: use the regular expression matching function ifrun ($ clsname, $ bf = 0) in linux {//. if a process is running, 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?

I hope this article will help you with php programming.

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.