Re: How to time out a forked command but still see output?
- From: "thecrow" <hokkaidocrow@xxxxxxxxx>
- Date: 16 Apr 2007 14:28:16 -0700
Never mind, I hit on something simple that worked. Don't know why I
didn't try this first. Haven't done Perl in years.
alarm($timeout);
open(CMD, "$command|");
while (<CMD>) {
print $_;
}
close CMD;
alarm(0);
The output is displayed up until the time of completion or timeout.
alarm() seems to interrupt this and its children just fine.
I'm sure the solution is obvious to everyone, but I'll record it here
for posterity for whoever else has the same question.
On Apr 16, 4:47 pm, "thecrow" <hokkaidoc...@xxxxxxxxx> wrote:
The goal... perl script launches the external program, shows all its
ouput in realtime. If too much time expires, perl script exits,
redirects all output of external program to some file. Can someone
give me a nudge in the right direction?
I tried a few things involving alarm() and eval but couldn't get them
to work. I won't waste your time with everything that failed, but the
following code is as close as I got. It is not acceptable because it
doesn't show the output of the program.
I also tried something like redirecting CMD to STDOUT but when I do
that, the output of the program keeps scrolling to the term even after
the timeout. I tried to solve this by closing these filehandles and
redirecting them to /dev/null outside of the eval, but those didn't
work either.
Help is appreciated...
#!/usr/bin/perl
$command = shift @ARGV;
$startupWait = shift @ARGV || 60;
eval {
local $SIG{'ALRM'} =
sub {
die "\nTimed out command $command after waiting
$startupWait seconds\n";
};
alarm($startupWait);
print "Running command: $command\n";
print "with timeout of $startupWait\n";
open(CMD, "$command|");
(@output) = (<CMD>);
close CMD;
alarm 0;
print "Command completed, output is:\n";
print map { "$_\n" } @output;};
die "$@" if ($@);
.
- Follow-Ups:
- References:
- How to time out a forked command but still see output?
- From: thecrow
- How to time out a forked command but still see output?
- Prev by Date: How to time out a forked command but still see output?
- Next by Date: Re: Error connecting to remote database
- Previous by thread: How to time out a forked command but still see output?
- Next by thread: Re: How to time out a forked command but still see output?
- Index(es):
Relevant Pages
|