作为构建穷人看门狗并确保应用程序崩溃的一种方法(直到我弄清原因),我需要编写一个PHP
CLI脚本,该脚本将由cron每5百万次运行一次,以检查该进程仍在运行。
基于此页面,我尝试了以下代码,但是即使我使用虚假数据对其进行调用,它也始终返回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");
接下来,我尝试了这段代码 …
(exec("ps -A | grep -i 'lighttpd -D' | grep -v grep", $output);)
print $output;
…但是没有达到我的期望:
/tmp> ./mycron.phpcli
Arrayroot:/tmp>
FWIW,此脚本与PHP 5.2.5的CLI版本一起运行,并且操作系统为uClinux 2.6.19.3。
谢谢您的提示。
编辑:这似乎工作正常
exec("ps aux | grep -i 'lighttpd -D' | grep -v grep", $pids);
if(empty($pids)) {
print "Lighttpd not running!\n";
} else {
print "Lighttpd OK\n";
}