Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??
What I really want to do is call B from another Perl script A, and A kills B (with system("kill PID") or whatever), thereby killing C as well.

One way to do that is to localize the HUP signal handler and then send a HUP signal to your process group. You can have A do

{ local $SIG{HUP} = 'IGNORE'; kill HUP => -$$; }

I've attached a proof of concept demo. Sorry there's no backport, I got lazy once I had it working.

a.pl
#!/opt/perl/bin/perl use 5.012; use warnings; say "A start"; my $pid; unless ( $pid = fork ) { die "cannot fork: $!" unless defined $pid; system("./b.pl"); sleep 30; exit; } sleep 2; say "A sending kill HUP -$$"; { local $SIG{HUP} = 'IGNORE'; kill HUP => -$$; } say "A waiting for lost children"; while (1) { my $kid = waitpid -1, 0; $kid == -1 and last; say "A reaped child $kid"; } say "A end";
b.pl
#!/opt/perl/bin/perl use 5.012; use warnings; say " B start"; my $pid; unless ($pid = fork) { die "cannot fork: $!" unless defined $pid; system("./c.pl"); sleep 30; exit; } say " B waiting on lost children"; while (1) { my $kid = waitpid -1, 0; $kid == -1 and last; say " B reaped child $kid"; } sleep 30; say " B end";
c.pl
#!/opt/perl/bin/perl use 5.012; use warnings; say " C start"; sleep 30; say " C exit";
output
$ ./a.pl A start B start B waiting on lost children C start A sending kill HUP -20682 A waiting for lost children A reaped child 20683 A end

In reply to Re: Stopping subprocesses by rowdog
in thread Stopping subprocesses by James Board

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (11)
    As of 2016-09-30 16:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Extraterrestrials haven't visited the Earth yet because:







      Results (570 votes). Check out past polls.