search
Homephp教程php手册用php守护另一个php进程的例子

这篇文章主要介绍了用php守护另一个php进程的例子,本文直接给出实现代码,需要的朋友可以参考下

要用php守护另一个php进程(apache模块的运行的,还有nginx等运行的除外)
a.php要守护b.php

在b.php中 通过 getmypid()函数获取当前进程的id,并将id写入c.pid文件中,如果程序执行完成将c.pid文件删除或清空

在a.php中 验证c.pid是否存在 ,是否为空,如果不为空,将pid读出,,通过exec执行 ps -p pid|grep 文件名来判断是否运行,判断后执行相应操作

可能有人要问,为什么不直接 ps aux|grep 文件名,这里主要是考虑到文件重名的情况下会出问题

a.php 代码

复制代码 代码如下:


$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代码

复制代码 代码如下:


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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function