Example of using PHP to guard another PHP process

Source: Internet
Author: User
Tags php code

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);
}
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.