作为一种构建穷人监视器的方法,并确保应用程序重新启动以防它崩溃(直到我找出原因),我需要编写一个PHP CLI脚本,每隔5mn将由cron运行以检查进程是否正常还在运行
基于此页面,我尝试了以下代码,但即使我用伪造数据调用它也总是返回True:
function processExists($file = false) {
$exists= false;
$file= $file ? $file : __FILE__;
// Check if file is in process list
exec("ps -C $file -o pid=", $pids);
if (count($pids) > 1) {
$exists = true;
}
return $exists;
}
#if(processExists("lighttpd"))
if(processExists("dummy"))
print("Exists\n")
else
print("Doesn't exist\n");
Run Code Online (Sandbox Code Playgroud)
接下来,我尝试了这段代码 ......
(exec("ps -A | grep -i 'lighttpd -D' | grep -v grep", $output);)
print $output;
Run Code Online (Sandbox Code Playgroud)
......但是没有达到我的期望:
/tmp> ./mycron.phpcli
Arrayroot:/tmp>
Run Code Online (Sandbox Code Playgroud)
FWIW,此脚本使用PHP 5.2.5的CLI版本运行,操作系统是uClinux 2.6.19.3.
谢谢你的提示.
编辑:这似乎工作正常
exec("ps aux | grep …Run Code Online (Sandbox Code Playgroud)