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

WBOY
Release: 2016-07-13 10:06:39
Original
814 people have browsed it

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...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template