Re: How to time out a forked command but still see output?



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


.



Relevant Pages

  • Re: alarm() under W2003
    ... there is no hook into a command line prompt for standard input. ... An alarm is triggered but Perl does nothing with this, ... To work around this lack of effective programming by Perl porters, ...
    (comp.lang.perl.misc)
  • Re: How to time out a forked command but still see output?
    ... If too much time expires, perl script exits, ... I tried a few things involving alarm() and eval but couldn't get them ... I also tried something like redirecting CMD to STDOUT but when I do ... die "\nTimed out command $command after waiting ...
    (comp.lang.perl.misc)
  • Re: waitpid and alarms
    ... > waitpid and alarms in perl 5.8.0. ... Forking pid -1856611 ... Bad command or file name ... print "Alarm signal\n"; ...
    (comp.lang.perl.misc)
  • Re: Wait for background processes to complete
    ... To be able to execute commands in the background and wait for their ... The documentation I am referring to is http://perldoc.perl.org/. ... You can run a command in the background with: ... There is a general problem with perl documentation: ...
    (comp.lang.perl.misc)
  • Re: Perl For Amateur Computer Programmers
    ... >professional computer programmers could use with the same ease as Basic. ... >Perl For Amateur Computer Programmers ... Also, taking into account that you're appealing to "scientists", it ... Also, as a side note, you seem to use the noun "command" in a naive ...
    (comp.lang.perl.misc)