note 87894 modified in function.proc-get-status by danbrown
| From: | danbrown@php.net | Date: | Mon, 29 Dec 2008 18:49:46 +0000 |
| Subject: | note 87894 modified in function.proc-get-status by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-148644@lists.php.net to get a copy of this message | ||
I have had lots of problems in the past bit trying to kill external commands run by proc_open.
Others have suggested using ps to find the children of the pid returned by proc_get_status, but on
my system this doesn't work. I'm using php-5.2.5 and apache-2.0.59 on linux kernel
2.6.21, and the processes I start with proc_open end up being owned by init (pid 1), not by the pid
returned by proc_get_status.
I did notice, however, that the pid's of the processes were always above and very close to the
proc_get_status pid. Using that information, I wrote a little function that takes the name of a
command, the starting pid at which to search (which would be the proc_get_status pid), and
optionally a search limit as arguments. It will use ps to list processes owned by apache (you may
have to change this user name for your system), and search for the command specified. The limit
tells how far above the starting pid to search. This will help if the command may have already
exited, and you don't want to kill a process from a different session than the one you're
working with.
Here's the code:
<?php
function findCommandPID($command, $startpid, $limit = 3)
{
$ps =
ps -u apache --sort=pid -o comm= -o pid=;
$ps_lines = explode("\n", $ps);
$pattern = "/(\S{1,})(\s{1,})(\d{1,})/";
foreach($ps_lines as $line)
{
if(preg_match($pattern, $line, $matches))
{
//this limits us to finding the command within $limit pid's of the parent;
//eg, if ppid = 245, limit = 3, we won't search past 248
if($matches[3] > $startpid + $limit)
break;
//try to match a ps line where the command matches our search
//at a higher pid than our parent
if($matches[1] == $command && $matches[3] > $startpid)
return $matches[3];
}
}
return false;
}
?>
--was--
I have had lots of problems in the past bit trying to kill external commands run by proc_open.
Others have suggested using ps to find the children of the pid returned by proc_get_status, but on
my system this doesn't work. I'm using php-5.2.5 and apache-2.0.59 on linux kernel
2.6.21, and the processes I start with proc_open end up being owned by init (pid 1), not by the pid
returned by proc_get_status.
I did notice, however, that the pid's of the processes were always above and very close to the
proc_get_status pid. Using that information, I wrote a little function that takes the name of a
command, the starting pid at which to search (which would be the proc_get_status pid), and
optionally a search limit as arguments. It will use ps to list processes owned by apache (you may
have to change this user name for your system), and search for the command specified. The limit
tells how far above the starting pid to search. This will help if the command may have already
exited, and you don't want to kill a process from a different session than the one you're
working with.
Here's the code:
<?
function findCommandPID($command, $startpid, $limit = 3)
{
$ps = ps -u apache --sort=pid -o comm= -o pid=;
$ps_lines = explode("\n", $ps);
$pattern = "/(\S{1,})(\s{1,})(\d{1,})/";
foreach($ps_lines as $line)
{
if(preg_match($pattern, $line, $matches))
{
//this limits us to finding the command within $limit pid's of the parent;
//eg, if ppid = 245, limit = 3, we won't search past 248
if($matches[3] > $startpid + $limit)
break;
//try to match a ps line where the command matches our search
//at a higher pid than our parent
if($matches[1] == $command && $matches[3] > $startpid)
return $matches[3];
}
}
return false;
}
?>
http://php.net/manual/en/function.proc-get-status.php