How to check whether specified PID is currently running without invoking ps from PHP?

后端 未结 9 1860
时光说笑
时光说笑 2020-12-15 03:29

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.

相关标签:
9条回答
  • 2020-12-15 04:02

    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");
    }
    
    0 讨论(0)
  • 2020-12-15 04:02
    //For Linux
    $pid='475678';
    exec('ps -C php -o pid', $a);
    if(in_array($pid, $a)){
        // do something...
    }
    
    0 讨论(0)
  • 2020-12-15 04:08
    $pid = 12345;
    if (shell_exec("ps ax | grep " . $pid . " | grep -v grep | wc -l") > 0)
    {
        // do something
    }
    
    0 讨论(0)
  • 2020-12-15 04:09

    If you are on Linux, try this :

    if (file_exists( "/proc/$pid" )){
        //process with a pid = $pid is running
    }
    
    0 讨论(0)
  • 2020-12-15 04:10

    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 讨论(0)
  • 2020-12-15 04:11

    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.

    0 讨论(0)
提交回复
热议问题