在bash脚本中,我想执行以下操作(在伪代码中):
if [ a process exists with $PID ]; then
kill $PID
fi
Run Code Online (Sandbox Code Playgroud)
条件语句的恰当表达式是什么?
作为一种构建穷人监视器的方法,并确保应用程序重新启动以防它崩溃(直到我找出原因),我需要编写一个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) 我想停止执行BASH脚本,直到进程关闭(我将PID存储在变量中).我在想
__CODE__
我见过的大多数例子都使用/ dev/null,它似乎需要root.有没有办法在不需要root的情况下执行此操作?
非常感谢你提前!