in reply to Re: Killing children's of children
in thread Killing children's of children

I'd second what pc88mxer suggested. — Here's a demo snippet illustrating the idea (Linux version):

#!/usr/bin/perl my $sid = getppid; # for the 'ps' process selection only if (my $pid = fork()) { sleep 1; # wait somewhat until tree of children is set up # show related processes system "ps f -s $sid -o pid,ppid,pgrp,sid,cmd"; # kill process group after timeout sleep 10; kill -9, $pid; wait; print "killed children\n"; # show remaining processes system "ps f -s $sid -o pid,ppid,pgrp,sid,cmd"; # do something else... sleep 10; } elsif (defined $pid) { # create new process group setpgrp; # run something (consisting of several processes) that hangs system 'bash -c "cat; echo"'; exit; } else { die "couldn't fork"; }

Sample output:

$ ./698259.pl PID PPID PGRP SID CMD 2963 2960 2963 2963 bash 4074 2963 4074 2963 \_ /usr/bin/perl ./698259.pl 4075 4074 4075 2963 \_ /usr/bin/perl ./698259.pl 4076 4075 4075 2963 | \_ bash -c cat; echo 4077 4076 4075 2963 | \_ cat 4078 4074 4074 2963 \_ ps f -s 2963 -o pid,ppid,pgrp,sid,cmd killed children PID PPID PGRP SID CMD 2963 2960 2963 2963 bash 4074 2963 4074 2963 \_ /usr/bin/perl ./698259.pl 4079 4074 4074 2963 \_ ps f -s 2963 -o pid,ppid,pgrp,sid,cmd

As you can see in the PGRP column, the three child processes in question all belong to the same process group (here 4075).

The idea should in principle even be portable to Windows (never tried it myself though...). As BrowserUk recently noted, that would be Win32::CreateProcess with CREATE_NEW_PROCESS_GROUP.

Replies are listed 'Best First'.
Re^3: Killing children's of children
by BrowserUk (Patriarch) on Jul 17, 2008 at 13:58 UTC