Example of using PHP to guard another PHP process

Source: Internet
Author: User

To use PHP to protect another PHP process (the Apache module is running, and nginx, etc.)

A.php to guard b.php.

Gets the ID of the current process through the getmypid () function in b.php, writes the ID to the C.pid file, and deletes or clears the C.pid file if the program executes

In a.php verify the existence of c.pid, whether it is empty, if not empty, the PID read out, through exec ps-p pid|grep file name to determine whether to run, after the decision to perform the appropriate action

Some people may ask, why not directly PS Aux|grep filename, this is mainly to consider the case of the file name will be a problem

a.php Code

The code is as follows:
?
$id =intval ($argv [1]);
if (!file_exists (' pid '. $id. ') PID ')) {
echo "Not run";
Exit
}
$content =file_get_contents (' pid '. $id. PID ');
if (empty ($content)) {
echo "Not run";
Exit
}
EXEC ("PS P". $content. ' | grep b.php ', $pids);
if (count ($pids) >0) echo (' runing ');
Else{echo ' not run ';}
?>


b.php Code

The code is as follows:
?

$id =intval ($argv [1]);
if (empty ($id)) exit;
File_put_contents (' pid '. $id. ') PID ', Getmypid ());
while (1) {
File_put_contents (' pid '. $id. ') PID ', Getmypid ());
Sleep (100);
}
?>

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.