J
J. Romano
Dear Perl community,
I've written some Perl code that runs the Unix "finger" command
inside of backticks (in order to capture its output). Occasionally
the finger command will hang for about thirty seconds or so (in which
case it almost always fails).
I want my Perl code to only wait three seconds for the finger
command to finish. After that, if the finger command has not
finished, I want to just discard the output and move on. Therefore, I
made use of the alarm() function:
$SIG{CHLD} = 'IGNORE'; # avoid creating zombie processes
# This next part (the eval/die blocks) is
# right out of "perldoc -f alarm":
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm 3; # wait a maximum of three seconds
$text = `finger some_user\@some_host`;
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
# Timed out:
print "Timed out!\n";
}
Now, this is difficult to test because the finger command almost
always works. However, in the rare case where it doesn't work, my
script prints "Timed out!" (as it should) and continues on until it
finishes.
The problem is that the timed-out finger command is apparently
still running even after my Perl script that called it finishes
running, because a short time later (like thirty seconds or a minute
later), an error message pops up on my terminal saying something like
"Unable to connect to host ...".
Now, I explicitly set $SIG{ENV} to 'IGNORE' so that this wouldn't
happen, but now I think that setting $SIG{ENV} to 'IGNORE' only
terminates (that is, reaps) child processes that have finished BEFORE
the Perl script has finished. (If I'm wrong in saying this, please
correct me.) But if the child process created by the backtick
operator finishes AFTER my Perl script finishes, it stays around well
longer than needed.
To summarize, my question is: If I use the alarm() function to
break out of a command that I started with the backtick operator, how
do I force that child process to exit so that it doesn't outlive my
Perl script?
In case anyone is interested, here is the output of "perl -v":
This is perl, v5.6.1 built for alpha-netbsd
Thanks in advance for any help,
J.
I've written some Perl code that runs the Unix "finger" command
inside of backticks (in order to capture its output). Occasionally
the finger command will hang for about thirty seconds or so (in which
case it almost always fails).
I want my Perl code to only wait three seconds for the finger
command to finish. After that, if the finger command has not
finished, I want to just discard the output and move on. Therefore, I
made use of the alarm() function:
$SIG{CHLD} = 'IGNORE'; # avoid creating zombie processes
# This next part (the eval/die blocks) is
# right out of "perldoc -f alarm":
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm 3; # wait a maximum of three seconds
$text = `finger some_user\@some_host`;
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
# Timed out:
print "Timed out!\n";
}
Now, this is difficult to test because the finger command almost
always works. However, in the rare case where it doesn't work, my
script prints "Timed out!" (as it should) and continues on until it
finishes.
The problem is that the timed-out finger command is apparently
still running even after my Perl script that called it finishes
running, because a short time later (like thirty seconds or a minute
later), an error message pops up on my terminal saying something like
"Unable to connect to host ...".
Now, I explicitly set $SIG{ENV} to 'IGNORE' so that this wouldn't
happen, but now I think that setting $SIG{ENV} to 'IGNORE' only
terminates (that is, reaps) child processes that have finished BEFORE
the Perl script has finished. (If I'm wrong in saying this, please
correct me.) But if the child process created by the backtick
operator finishes AFTER my Perl script finishes, it stays around well
longer than needed.
To summarize, my question is: If I use the alarm() function to
break out of a command that I started with the backtick operator, how
do I force that child process to exit so that it doesn't outlive my
Perl script?
In case anyone is interested, here is the output of "perl -v":
This is perl, v5.6.1 built for alpha-netbsd
Thanks in advance for any help,
J.