How can I know if a given PID is running ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nelson777
Forum Newbie
Posts: 3
Joined: Tue May 16, 2006 2:36 pm

How can I know if a given PID is running ?

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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;
}
nelson777
Forum Newbie
Posts: 3
Joined: Tue May 16, 2006 2:36 pm

Post by nelson777 »

Actually I'm under Windows :(
proc_open and proc_status, don't work in my case. Thnx anyway.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

nelson777
Forum Newbie
Posts: 3
Joined: Tue May 16, 2006 2:36 pm

Post 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
Post Reply