Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a process is running using PHP in Linux

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.

like image 406
hsinxh Avatar asked Jun 19 '11 16:06

hsinxh


People also ask

How to check if a process is running in PHP?

In PHP this can be retrieved using getmypid() which will return an integer number. This pid number can be saved to a file and each time the script is run a check made to see if the file exists. If it is the posix_kill() function can be used to see if a process is running with that pid number.

How to check if PHP function is working?

Determining if a PHP function is available You can determine whether or not a PHP function is enabled for your web site by using the function_exists() function.

How do I know if pid is running on Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


1 Answers

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";
}
like image 141
Mat Avatar answered Oct 23 '22 08:10

Mat