36

We would like to check if a specified process is currently running via PHP.

We would like to simply supply a PID and see if it is currently executing or not.

Does PHP have an internal function that would give us this information or do we have to parse it out of "ps" output?

kenorb
  • 137,499
  • 74
  • 643
  • 694
anonymous-one
  • 13,444
  • 18
  • 56
  • 81

9 Answers9

80

If you are on Linux, try this :

if (file_exists( "/proc/$pid" )){
    //process with a pid = $pid is running
}
Nasreddine
  • 35,089
  • 17
  • 75
  • 92
47

posix_getpgid($pid); will return false when a process is not running

Wandering Zombie
  • 1,031
  • 11
  • 13
  • 2
    This works for my purposes - setting a lock preventing the same script from running while a pid exists in a lock table. Thx. – dmgig May 02 '14 at 15:06
  • As @jami said, `posix_*` functions are the way to go! For example, the accepted answer doesn't work on OSX because OSX doesn't have `/proc` folder! – parse Apr 28 '20 at 12:54
  • `posix_*` will NOT do the job if your user has no access to the given pid. See the comment from @webmaster777 in the answer from @steel-brain. – SeparateReality Mar 15 '21 at 18:12
20

If you want to have a function for it then:

$running = posix_kill($pid,0);

Send the signal sig to the process with the process identifier pid.

Calling posix_kill with the 0 kill signal will return true if the process is running, false otherwise.

kenorb
  • 137,499
  • 74
  • 643
  • 694
Steel Brain
  • 4,042
  • 26
  • 38
  • 1
    This will not work for processes the user does not have access to (e.g. a daemon running as `root`, while the user running the script is `www-data`) (see https://stackoverflow.com/a/15774758/593868) – webmaster777 Sep 06 '17 at 13:29
2

I would call a bash script using shell_exec

$pid = 23818;
if (shell_exec("ps aux | grep " . $pid . " | wc -l") > 0)
{
    // do something
}
Pierre-Olivier
  • 3,086
  • 17
  • 37
1

I think posix_kill(posix_getpgrp(), 0) is the best way to check if PID is running, it's only not available on Windows platforms.

It's the same to kill -0 PID on shell, and shell_exec('kill -0 PID') on PHP but NO ERROR output when pid is not exists.

In forked child process, the posix_getpgid return parent's pid always even if parent was terminated.

<?php

$pid = pcntl_fork();

if ($pid === -1) {
    exit(-1);
} elseif ($pid === 0) {
    echo "in child\n";
    while (true) {
        $pid = posix_getpid();
        $pgid = posix_getpgid($pid);
        echo "pid: $pid\tpgid: $pgid\n";
        sleep(5);
    }
} else {
    $pid = posix_getpid();
    echo "parent process pid: $pid\n";
    exit("parent process exit.\n");
}
consatan
  • 319
  • 1
  • 7
  • 18
0

i have done a script for this, which im using in wordpress to show game-server status, but this will work with all running process on the server

<?php
//##########################################
// desc: Diese PHP Script zeig euch ob ein Prozess läuft oder nicht
// autor: seevenup
// version: 0.2
//##########################################

if (!function_exists('server_status')) {
        function server_status($string,$name) {
                $pid=exec("pidof $name");
                exec("ps -p $pid", $output);

                if (count($output) > 1) {
                        echo "$string: <font color='green'><b>RUNNING</b></font><br>";
                }
                else {
                        echo "$string: <font color='red'><b>DOWN</b></font><br>";
                }
        }
}

//Beispiel "Text zum anzeigen", "Prozess Name auf dem Server"
server_status("Running With Rifles","rwr_server");
server_status("Starbound","starbound_server");
server_status("Minecraft","minecarf");
?>

more information here http://umbru.ch/?p=328

0
//For Linux
$pid='475678';
exec('ps -C php -o pid', $a);
if(in_array($pid, $a)){
    // do something...
}
  • Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 29 '16 at 19:40
0

Here is how we do it:

if (`ps -p {$pid} -o comm,args=ARGS | grep php`) {

  //process with pid=$pid is running;
}
Denis Matafonov
  • 2,498
  • 18
  • 29
0
$pid = 12345;
if (shell_exec("ps ax | grep " . $pid . " | grep -v grep | wc -l") > 0)
{
    // do something
}
Leonid Zakharov
  • 904
  • 1
  • 6
  • 11