I am using kannel to send SMS via PHP. I want to know how can I check if a particular process is running. For kannel to run, a process named bearerbox
should 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发送SMS。我想知道如何检查某个进程是否正在运行。要让kannel运行,一个名为bearerbox的进程应该一直运行。我想检查这个过程是否正在运行。因为如果进程没有运行,那么会有邮件发送给我,通知我。
5 个解决方案
#1
24
The easiest is to use pgrep
, which has an exit code of 0 if the process exists, 1 otherwise.
最简单的方法是使用pgrep,如果进程存在,pgrep的出口代码为0,否则为1。
Here's an example.
这是一个例子。
exec("pgrep bearerbox", $output, $return);
if ($return == 0) {
echo "Ok, process is running\n";
}
#2
5
You can use the exec command to find your process and then act accordingly.
您可以使用exec命令找到您的流程,然后相应地进行操作。
Something like:
喜欢的东西:
exec('ps aux | grep bearerbox', $output);
执行主任('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.
祝你好运。
#3
4
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.
不过,Deamons总是会创建一个“pid”文件。这个文件包含守护进程的进程id。如果您的进程有这个id,您可以检查文件的内容,并查看具有该id的进程是否仍在运行。这是更可靠。
supervisord might 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.
monitor sord也可能具有此功能。最后,也许最好是建立一个真正的监控系统,而不是自己去做。Nagios可能是一个不错的选择,但也可能有其他的。
#4
2
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(' ', sys_getloadavg());
$output=sprintf('<table data-cpu="%s" id="process">%s</table>',$cpu, $output);
#5
0
This is the best way
这是最好的办法
<?php
exec("ps -eo comm,pid | awk '$1 == "."\"gs\""." { print $2 }'", $output);
if ($output != 0) {
echo "The process gs is running\n";
}
?>
in the above code gs is the process that I was checking
在上面的代码中,g是我检查的过程
#1
24
The easiest is to use pgrep
, which has an exit code of 0 if the process exists, 1 otherwise.
最简单的方法是使用pgrep,如果进程存在,pgrep的出口代码为0,否则为1。
Here's an example.
这是一个例子。
exec("pgrep bearerbox", $output, $return);
if ($return == 0) {
echo "Ok, process is running\n";
}
#2
5
You can use the exec command to find your process and then act accordingly.
您可以使用exec命令找到您的流程,然后相应地进行操作。
Something like:
喜欢的东西:
exec('ps aux | grep bearerbox', $output);
执行主任('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.
祝你好运。
#3
4
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.
不过,Deamons总是会创建一个“pid”文件。这个文件包含守护进程的进程id。如果您的进程有这个id,您可以检查文件的内容,并查看具有该id的进程是否仍在运行。这是更可靠。
supervisord might 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.
monitor sord也可能具有此功能。最后,也许最好是建立一个真正的监控系统,而不是自己去做。Nagios可能是一个不错的选择,但也可能有其他的。
#4
2
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(' ', sys_getloadavg());
$output=sprintf('<table data-cpu="%s" id="process">%s</table>',$cpu, $output);
#5
0
This is the best way
这是最好的办法
<?php
exec("ps -eo comm,pid | awk '$1 == "."\"gs\""." { print $2 }'", $output);
if ($output != 0) {
echo "The process gs is running\n";
}
?>
in the above code gs is the process that I was checking
在上面的代码中,g是我检查的过程