[ACCEPTED]-Check if a process is running using PHP in Linux-kannel

Accepted answer
Score: 27

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

Here's 1 an example.

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

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

Something 3 like:

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

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

Good luck.

Score: 4

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

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

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

Score: 3

Simple yet handy solution to monitor processes 1 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);
foreach ($head AS $f) $output.="<td class=\"head\">$f</td>";
$output=sprintf('<tr class="head">%s</tr>',$output);
foreach($processes AS $p){
    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);
$cpu=implode('&nbsp;&nbsp;&nbsp;', sys_getloadavg());
$output=sprintf('<table data-cpu="%s" id="process">%s</table>',$cpu, $output);
Score: 0

This is the best way

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 1 is the process that I was checking

More Related questions