12

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
hsinxh
  • 1,865
  • 6
  • 21
  • 25
  • you could use a php exec function – ldg Jun 19 '11 at 16:15
  • Make sure it's not possible for external users to modify your exec() calls, hardcode them if possible. Bad code can give evil minded people access to the whole system. – Mattis Jun 19 '11 at 16:28
  • @mattis , yeah sure I will keep that in Mind. @ldg thanks for suggestion buddy, thats exactly what I will be using. – hsinxh Jun 19 '11 at 16:32
  • Possible duplicate of [Checking if process still running?](https://stackoverflow.com/questions/3111406/checking-if-process-still-running) – kenorb Jul 04 '17 at 12:21

5 Answers5

27

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

Here's an example.

exec("pgrep bearerbox", $output, $return);
if ($return == 0) {
    echo "Ok, process is running\n";
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • You may want to check, but from what I found the exit codes are the opposite way around- 1 if it exists; 0 if not. Can anyone clarify on this? – Matt Fletcher Jul 01 '13 at 08:48
  • @MattFletcher: pgrep fails (rc=1) if no process is found with that name (just tried it out). – Mat Jul 01 '13 at 08:52
  • Through shell, yes, but I just tried it out in PHP CLI and it does it the opposite way around. `$output` will return an array, empty or with each PID. Echoing the `exec()` command will return the PID or null. `$return` returned 0 if the process doesn't exist and 1 if it does! – Matt Fletcher Jul 01 '13 at 09:05
  • Ps: This is using PHP 5.4.9 on Ubuntu 13 – Matt Fletcher Jul 01 '13 at 09:06
  • Update: I think maybe just the timing on the commands I was running was a bit glitchy, my bad... It is the original way around described. Thought it was weird because I've seen the whole "exit code is 0 if it exists" thing in C before :) – Matt Fletcher Jul 01 '13 at 09:13
  • @MattFletcher: that's odd. I'm getting `$return == 0` for existing processes, 1 otherwise (PHP 5.4.8 on RedHat). – Mat Jul 01 '13 at 09:17
5

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

Something like:

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.

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'.

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.

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
3

Simple yet handy solution to monitor processes through 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);
kenorb
  • 155,785
  • 88
  • 678
  • 743
Samer Ata
  • 1,027
  • 1
  • 12
  • 11
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

Kiran Reddy
  • 734
  • 12
  • 28