in reply to Stopping subprocesses

You might want to look at Killing children's of children and Proc::Killfam. On linux anyways, sometimes when you fork, you get the pid of the shell which is spawned to run your child, so there is an hidden pid in there. Killfam will find them all, and kill them.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku flash japh

Replies are listed 'Best First'.
Re^2: Stopping subprocesses
by James Board (Beadle) on Aug 24, 2010 at 14:57 UTC
    Based on the documentation for Proc::Killfam, this won't work. If B finishes before C, then the process table will "re-parent" C, and there will be no way of telling, from the process table, that C was a child of B. Unless I'm misunderstanding things, this can't work, right?
      If a process has disassociated itself from its parent (which is not as simple as you say), it won't be reached by any methods short killing it specifically with its PID. This is a straw man.
      then the process table will "re-parent" C,

      Are you making potential zombies? Otherwise it should reparent it just up 1 level in the process tree that spawned it.

      Have you seen the "f" switch for the ps command? It shows the "forrest" view of the process tree. Look at it, before and after B finishes, and see if the branch of the forest gets moved. Proc::Killfam will kill everything that branches off of the pid handed to it.

      You should be able to find the pid of the highest related process up the tree branch, and run Killfam on it, to get all the programs and associated shells.

      But maybe you have hit on an odd behavior?

      Here is an old manual routine that may help you.

      #!/usr/bin/perl use warnings; use strict; #robau #This subroutine takes two arguments, the parent process ID #and the numeric signal to pass to the processes #(which would be 9 if you wanted to issue a -TERM). #Using Proc::Process you could find the process ID #of the process login -- zentara with something similar #to the following : #my $proc = Proc::ProcessTable->new; #my @ps = map { $_->pid if ($_->cmndline =~ /login -- zentara/) } @{$p +roc->table}; #&killchd($_, 9) foreach @ps; &killchd(9895, 9); #kill -9 process 9895 sub killchd ($;$) { use Proc::ProcessTable; my $sig = ($_[1] =~ /^\-?\d+$/) ? $_[1] : 0; my $proc = Proc::ProcessTable->new; my %fields = map { $_ => 1 } $proc->fields; return undef unless exists $fields{'ppid'}; foreach (@{$proc->table}) { kill $sig, $_->pid if ($_->ppid == $_[0]); }; kill $sig, $_[0]; };

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku flash japh