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.


In reply to Re^2: Killing children's of children by almut
in thread Killing children's of children by scarus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.