Home > Article > Backend Development > Example of using php to guard another php process, guarding another php process_PHP tutorial
Use php to protect another php process (except those running the apache module, and nginx, etc.)
a.php needs to protect b.php
In b.php, get the id of the current process through the getmypid() function, and write the id into the c.pid file. If the program execution is completed, delete or clear the c.pid file
Verify in a.php whether c.pid exists and whether it is empty. If it is not empty, read out the pid, execute ps -p pid|grep file name through exec to determine whether it is running, and perform the corresponding operations after judgment.
Some people may ask, why not ps aux|grep the file name directly? The main reason here is to consider the problems that may arise when files have duplicate names
a.php code
$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);
}
?>