Page 1 of 1

How can I know if a given PID is running ?

Posted: Tue May 16, 2006 2:59 pm
by nelson777
How can I know if a given PID is running ? is there any function that can be used like this:

Code: Select all

if  (is_running($pid)) 
   do_something();
Thnx.

Re: How can I know if a given PID is running ?

Posted: Tue May 16, 2006 3:45 pm
by Chris Corbyn
nelson777 wrote:How can I know if a given PID is running ? is there any function that can be used like this:

Code: Select all

if  (is_running($pid)) 
   do_something();
Thnx.
You can try proc_open() in combination with proc_status().

Failing that, the following will work (unless the pid is hidden due to some sort of attack):

Code: Select all

function is_running($pid)
{
    $results = `ps aux`;
    $pattern = '@^\S+\ '.$pid.'\ @m';
    if (preg_match($pattern, $results)) return true;
    else return false;
}

Posted: Thu May 18, 2006 11:44 am
by nelson777
Actually I'm under Windows :(
proc_open and proc_status, don't work in my case. Thnx anyway.

Posted: Thu May 18, 2006 12:58 pm
by Chris Corbyn
nelson777 wrote:Actually I'm under Windows :(
proc_open and proc_status, don't work in my case. Thnx anyway.
Ah sorry, my windows knowledge when it comes to things like this is pretty poor. If you can run a command on the command line that does a `ps' equivalent (lists all processes) then the backtick syntax I used above, or shell_exec() should work if you're happy enough scanning the returned string.

Posted: Thu May 18, 2006 1:08 pm
by timvw

Posted: Mon May 22, 2006 12:29 pm
by nelson777
Solved it!

Following timvw' link, I get to know that there is a small command line tool in the support folder of W2000' installation CD called "tlist" that does the same as ps under unix. So adapting d11wtq code my function ended like this:

Code: Select all

function is_running($pid)
{
    $results = `tlist`;
    if ((strpos($results, $pid . "")) > 0) return true;
    else return false;
};
hope it's useful for someone else.

Thxnx,
Nelson