【发布时间】:2011-03-07 21:16:18
【问题描述】:
作为一种构建穷人看门狗并确保应用程序在崩溃时重新启动(直到我弄清楚原因)的一种方法,我需要编写一个 PHP CLI 脚本,该脚本将由 cron 每 500 万次运行一次以检查进程是否仍在运行。
基于this page,我尝试了以下代码,但即使我用虚假数据调用它也总是返回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");
接下来,我尝试了this code...
(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";
}
【问题讨论】: