Home >Backend Development >PHP Tutorial >Example of using php to protect another php process_PHP tutorial

Example of using php to protect another php process_PHP tutorial

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

Example of using php to protect another php process

To 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

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

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/958131.htmlTechArticleExample of using php to protect another php process. To use php to protect another php process (apache module is running, Except those running nginx etc.) a.php needs to protect b.php in b.php through getmy...
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