40

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
  • 155,785
  • 88
  • 678
  • 743
anonymous-one
  • 14,454
  • 18
  • 60
  • 84

10 Answers10

85

If you are on Linux, try this :

if (file_exists( "/proc/$pid" )){
    //process with a pid = $pid is running
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
50

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

Wandering Zombie
  • 1,101
  • 13
  • 14
  • 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
21

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
  • 155,785
  • 88
  • 678
  • 743
Steel Brain
  • 4,321
  • 28
  • 38
  • 2
    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,104
  • 19
  • 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
  • 349
  • 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,684
  • 23
  • 30
0

posix_getpgid is BEST

<?php
// cf. https://stackoverflow.com/questions/9874331/how-to-check-whether-specified-pid-is-currently-running-without-invoking-ps-from

$pid_max = file_get_contents('/proc/sys/kernel/pid_max');
$pids = array_map(fn()=>random_int(1, $pid_max), range(1,2**20));


$start = microtime(true);
foreach ($pids as $pid) {
    file_exists("/proc/$pid");
}
echo "file_exists:\t" . microtime(true) - $start . PHP_EOL;

$start = microtime(true);
foreach ($pids as $pid) {
    is_dir("/proc/$pid");
}
echo "is_dir:\t\t" . microtime(true) - $start . PHP_EOL;

$start = microtime(true);
foreach ($pids as $pid) {
    posix_getpgid($pid);
}
echo "posix_getpgid:\t" . microtime(true) - $start . PHP_EOL;

$start = microtime(true);
foreach ($pids as $pid) {
    posix_kill($pid, 0);
}
echo "posix_kill:\t" . microtime(true) - $start . PHP_EOL;
$ seq 10 | xargs -n1 php pid_perf.php
file_exists:    2.1520388126373
is_dir:         1.6568348407745
posix_getpgid:  0.12570405006409
posix_kill:     0.14235901832581
file_exists:    2.1547100543976
is_dir:         1.6315920352936
posix_getpgid:  0.12148284912109
posix_kill:     0.14736986160278
file_exists:    2.121838092804
is_dir:         1.6587860584259
posix_getpgid:  0.12204599380493
posix_kill:     0.13927102088928
file_exists:    2.1496820449829
is_dir:         1.6658518314362
posix_getpgid:  0.12524199485779
posix_kill:     0.14765810966492
file_exists:    2.1388080120087
is_dir:         1.6538898944855
posix_getpgid:  0.12666511535645
posix_kill:     0.14308190345764
file_exists:    2.0925102233887
is_dir:         1.7037389278412
posix_getpgid:  0.12445998191833
posix_kill:     0.13687419891357
file_exists:    2.0917370319366
is_dir:         1.6500859260559
posix_getpgid:  0.1252658367157
posix_kill:     0.14215397834778
file_exists:    2.0797710418701
is_dir:         1.6966879367828
posix_getpgid:  0.12555503845215
posix_kill:     0.14388394355774
file_exists:    2.099790096283
is_dir:         1.6563739776611
posix_getpgid:  0.12412095069885
posix_kill:     0.14297080039978
file_exists:    2.0959348678589
is_dir:         1.6471560001373
posix_getpgid:  0.12421989440918
posix_kill:     0.13587403297424
-1
$pid = 12345;
if (shell_exec("ps ax | grep " . $pid . " | grep -v grep | wc -l") > 0)
{
    // do something
}
Leonid Zakharov
  • 940
  • 1
  • 6
  • 11
  • dangerously wrong, if you try to track pid 77 like this, it will match pid 77, 577, 771, etcetc. real life example: https://pastebin.com/raw/LxK26Ukg – hanshenrik Jul 15 '22 at 13:44