[Linux-disciples] perl parent vs. child vs. process group -- killing

Adam Rosi-Kessel adam at rosi-kessel.org
Thu Aug 19 12:53:49 EDT 2004


On Wed, Aug 11, 2004 at 02:11:56PM -0400, Stephen R Laniel wrote:
> On Wed, Aug 11, 2004 at 01:59:58PM -0400, Dylan Thurston wrote:
> > Presumably there's some way to do this, since pstree does it.
> A quick scan through pstree.c didn't immediately reveal how
> pstree discovers the child pids; the scan reveals how it
> walks the linked list of child pids, but not how it figures
> out those pids to begin with. Perhaps others will have more
> luck than I did:

FWIW, the following subroutine seems to fix the problem, although I
consider it a hack and I'm concerned because I don't think 'ps' behaves
the same, even on all linux systems: 

sub KillAll {
  my $pid = shift;
  local $SIG{HUP} = 'IGNORE';
  if (open PS, "ps -o pid,ppid |") {
     foreach my $line (<PS>) {
       kill HUP => $1 if ($line =~ /^\s*(\d+)\s*(\d+)\s*$/ and ($1 eq $pid or $2 eq $pid));
     }
     close PS;
  } else {                      
    kill HUP => -$$;
  }
}

You call KillAll with a process ID and it kills that process and all its
children.  Can anyone suggest a better way to do this?  Or does this seem
like a reasonable way to accomplish the goal?
-- 
Adam Rosi-Kessel
http://adam.rosi-kessel.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20040819/e0e4604c/attachment.pgp


More information about the Linux-disciples mailing list