Check if a process is running using PHP in Linux

hsinxh picture hsinxh · Jun 19, 2011 · Viewed 24.7k times · Source

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.

Answer

Mat picture Mat · Jun 19, 2011

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";
}