在 Linux 中使用 PHP 检查进程是否正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:36:43  来源:igfitidea点击:

Check if a process is running using PHP in Linux

phplinuxdaemonkannel

提问by hsinxh

I am using kannelto send SMS via PHP. I want to know how can I check if a particular process is running. For kannelto run, a process named bearerboxshould be running all time. I want to check if this process is running or not. Because if the process is not running then a mail will be sent to me notifying me about it.

我正在使用kannel通过 PHP 发送短信。我想知道如何检查特定进程是否正在运行。对于的Kannel运行,一个名为过程bearerbox应运行的所有时间。我想检查这个进程是否正在运行。因为如果该进程没有运行,则会向我发送一封邮件通知我。

采纳答案by Mat

The easiest is to use pgrep, which has an exit code of 0 if the process exists, 1 otherwise.

最简单的方法是使用pgrep,如果进程存在,它的退出代码为 0,否则为 1。

Here's an example.

这是一个例子。

exec("pgrep bearerbox", $output, $return);
if ($return == 0) {
    echo "Ok, process is running\n";
}

回答by Evert

There are a lot of ways to deal with this. The easiest (and a direct answer to your question) is to grab the output of 'ps'.

有很多方法可以解决这个问题。最简单的(也是对您问题的直接回答)是获取 'ps' 的输出。

Deamons tend to always create a 'pid' file though. This file contains the process-id of the daemon. If yours has that, you can check the contents of the file and see if the process with that id is still running. This is more reliable.

不过,守护进程往往会创建一个“pid”文件。该文件包含守护进程的进程 ID。如果你有这个,你可以检查文件的内容,看看具有该 ID 的进程是否仍在运行。这个比较靠谱。

supervisordmight also have this functionality. Lastly, maybe it's better to get a real monitoring system rather than build something yourself. Nagios might be a good pick, but there might be others.

supervisord可能也有这个功能。最后,也许获得一个真正的监控系统比自己构建一些东西更好。Nagios 可能是一个不错的选择,但可能还有其他选择。

回答by Nathan Poole

You can use the exec command to find your process and then act accordingly.

您可以使用 exec 命令来查找您的进程,然后采取相应的行动。

Something like:

就像是:

exec('ps aux | grep bearerbox', $output);

exec('ps aux | grep bearerbox', $output);

You'll need to work out what is returned on your server to decide if it's running or not.

您需要计算服务器上返回的内容,以决定它是否正在运行。

Good luck.

祝你好运。

回答by Samer Ata

Simple yet handy solution to monitor processes through PHP: PHP-Linux-Process-Monitor.

通过 PHP 监控进程的简单而方便的解决方案:PHP-Linux-Process-Monitor

The code goals like:

代码目标如下:

$ps = explode("\n", trim(shell_exec('ps axo pid,ppid,%cpu,pmem,user,group,args --sort %cpu')));
foreach($ps AS $process){
$processes[]=preg_split('@\s+@', trim($process), 7 );
}
$head= array_shift($processes);
$processes = array_reverse($processes);
$output='';
foreach ($head AS $f) $output.="<td class=\"head\">$f</td>";
$output=sprintf('<tr class="head">%s</tr>',$output);
foreach($processes AS $p){
    $output.='<tr>';
    foreach ($p AS $i=>$f){
        if($i==0) $output.=sprintf('<td>%1$s</td>',$f);
        elseif($i==2) $output.=sprintf('<td class="cpu">%1$s<ins style="width:%1$s%%"></ins></td>',$f);
        elseif($i==3) $output.=sprintf('<td class="mem">%1$s<ins style="width="%1$s%%"></ins></td>',$f);
        elseif($i == 6) $output.=sprintf('<td class="command">%1$s</td>',$f);
        else $output.=sprintf('<td>%1$s</td>',$f);
    }
    $output.='</tr>';
}
$cpu=implode('&nbsp;&nbsp;&nbsp;', sys_getloadavg());
$output=sprintf('<table data-cpu="%s" id="process">%s</table>',$cpu, $output);

回答by Kiran Reddy

This is the best way

这是最好的方法

<?php
exec("ps -eo comm,pid | awk ' == "."\"gs\""." { print  }'", $output);
if ($output != 0) {
    echo "The process gs is running\n";
}
?>

in the above code gs is the process that I was checking

在上面的代码 gs 是我正在检查的过程