T
thecrow
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 ($@);
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 ($@);