Quote:> I have a problem in killing all the processes under a process.
> When I try to kill a Parent process, Child process and its
> child process are not being killed. I do manually by checking
> 'ps -ef |grep process' and then kill each process and its child
> process individually. It some times consumes lot of time for me
> and also sometimes I will be missing some sub-processes.
If the parent process is a process group leader, then this
is simple --- merely preceed its PID with a - and the whole
process group gets the signal:
kill -TERM -$pid
If it isn't a process group leader, and you have some control
over how the process family gets started, I'd suggest forcing
it to be the leader of a new process group. But if you can't
do that, then you'll have to resort to building a model of
the process tree and use that. I suppose the easiest would
be to use perl:
#! /usr/bin/perl
open(STDIN, "ps -ef |") || die "cannot start ps: $!\n";
while (<STDIN>) {
($junk, $pid, $ppid) = split;
}
my $pid = shift;
}
(Note that you might need to make changes to adapt this script
to the implementation of ps on your system. For instance, you
might need to use "ps ax" instead of "ps -ef", and use additional
"junk" variables on the "split" assignment.)
If that were placed in a script in your $PATH named "killtree",
it could be used as follows:
killtree $parentpid
--Ken Pizzini