Home » Php » process – How to check whether specified PID is currently running without invoking ps from PHP?

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

If you are on Linux, try this :

if (file_exists( "/proc/$pid" )){
    //process with a pid = $pid is running
}

Answer:

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

Answer:

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.

Answer:

I would call a bash script using shell_exec

$pid = 23818;
if (shell_exec("ps aux | grep " . $pid . " | wc -l") > 0)
{
    // do something
}

Answer:

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

Answer:

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
//

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

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

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

Answer:

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

Answer:

//For Linux
$pid='475678';
exec('ps -C php -o pid', $a);
if(in_array($pid, $a)){
    // do something...
}

Answer:

Here is how we do it:

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

  //process with pid=$pid is running;
}

Answer:

$pid = 12345;
if (shell_exec("ps ax | grep " . $pid . " | grep -v grep | wc -l") > 0)
{
    // do something
}