kill all the child processes

  • Follow


can any one give me the unix shell script to kill all the child
processes if a parent process id is given. The parent process id has
child processes and inturn each child process can have their child
processes and so on. if the parent process id is given it should
identify all its child process and their child processes and kill all
the processes starting from the lowest level.

0
Reply ajayreddy.1 (1) 12/18/2005 12:01:24 AM

"ajju" <ajayreddy.1@gmail.com> wrote:
# can any one give me the unix shell script to kill all the child
# processes if a parent process id is given. The parent process id has
# child processes and inturn each child process can have their child
# processes and so on. if the parent process id is given it should
# identify all its child process and their child processes and kill all
# the processes starting from the lowest level.

A simpler trick is to kill a process group.

As stated, you can either do funky system specific stuff (/proc on
some unices, other techniques on other unices), or a slightly less
system specific filterring of the output of ps (though ps also
varies on different systems) to gather ppid for each pid and build
a process graph. (Some ps implementation also do this.) And then
walk the graph, killing the pids.

Note that a orphanned process becomes a child of init and you lose
any (great-)*grandparent associations.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.
0
Reply SM 12/18/2005 10:39:53 AM


On 17 Dec 2005 16:01:24 -0800, "ajju" <ajayreddy.1@gmail.com> wrote:

>can any one give me the unix shell script to kill all the child
>processes if a parent process id is given. The parent process id has
>child processes and inturn each child process can have their child
>processes and so on. if the parent process id is given it should
>identify all its child process and their child processes and kill all
>the processes starting from the lowest level.

Here is a Perl script that does it, you probably can translate
it to a shell of your chosing. Basically you loop through the process
table looking for the parent pid.  If you like using Perl, there is also
Proc::Killfam, which does about the same thing.

########################################################
#!/usr/bin/perl
use warnings;
use strict;
#by 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/) }
@{$proc->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];
};
__END__


-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
0
Reply zentara 12/18/2005 11:58:54 AM

2 Replies
951 Views

(page loaded in 0.043 seconds)

Similiar Articles:













7/23/2012 12:05:41 AM


Reply: