Home  >  Article  >  Backend Development  >  Example of using php to guard another php process, guarding another php process_PHP tutorial

Example of using php to guard another php process, guarding another php process_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:39808browse

An example of using php to protect another php process, to protect another php process

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

Copy 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(‘running’);
else{echo ‘not run’;}
?>

b.php code
Copy 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);
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/958254.htmlTechArticleAn example of using php to protect another php process. To protect another php process, use php to protect another php process ( The apache module is running, except those running nginx etc.) a.php needs to protect b.p...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn