An often overlooked concept are process group IDs.  Just create a new process group before you fork. Then, independently of how many children are being forked, and how deep their fork/exec level gets, you can address all of them in one go.  A short demo:

#!/usr/bin/perl my $sid = getppid; # for the 'ps' 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 5; 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 5; } 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"; } __END__ Sample output: $ ./726031.pl PID PPID PGRP SID CMD 29171 29169 29171 29171 bash -rcfile .bashrc 508 29171 508 29171 \_ /usr/bin/perl ./726031.pl 509 508 509 29171 \_ /usr/bin/perl ./726031.pl 510 509 509 29171 | \_ bash -c cat; echo 511 510 509 29171 | \_ cat 512 508 508 29171 \_ ps f -s 29171 -o pid,ppid,pgrp,sid,cmd killed children PID PPID PGRP SID CMD 29171 29169 29171 29171 bash -rcfile .bashrc 508 29171 508 29171 \_ /usr/bin/perl ./726031.pl 513 508 508 29171 \_ ps f -s 29171 -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 509). The single kill addressed to this group gets rid of all three processes.

The only issue you might run into is when some process further down the line itself creates another new process group. But as the concept is largely unknown/unused, the chances you'll encounter that problem in practice are rather low :)  At least, it's worth a try.


In reply to Re: Fork daemon from Perl script and save it's PID. How to do it right? by almut
in thread Fork daemon from Perl script and save it's PID. How to do it right? by accessdenied

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.