php 判断进程是否存在,检查进程是否仍在运行?

作为构建穷人看门狗并确保应用程序崩溃的一种方法(直到我弄清原因),我需要编写一个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";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值