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