2 methods of using PHP to judge the running state of a program in Linux

Source: Internet
Author: User

This article mainly introduces the Linux in PHP to determine the state of the program running 2 methods, the need for friends can refer to the

Sometimes in the server to write some scripts, often to put into the crontab inside the regular operation. A long time there is a problem, that is, the program repeatedly run consumption of too much resources, how to deal with it? Here's a couple of ways I've written:

The first: a regular match in Linux

The code is as follows:

function Ifrun ($clsname, $BF = 0)

{

The following detects that if a process is running, it does not 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:

The code is as follows:

if (Ifrun (' Pooy ', 5)) {Die ("Pooy is Running");}

Note: Pooy is the name of the program pooy.php!

The second: write the process to the file, then use the file function to read and then match the string

The code is as follows:

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) {

The echo ' Earth ' is 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 within the program!

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.