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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|