Php judges program problem instances in linux, and php judges linux instances.

Source: Internet
Author: User

Php judges program problem instances in linux, and php judges linux instances.

This article describes how php judges programs in linux. 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.