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



On Apr 21, 10:06 pm, "comp.llang.perl.moderated" <c...@blv-
sam-01.ca.boeing.com> wrote:
On Apr 21, 8:58 am, thecrow <hokkaidoc...@xxxxxxxxx> wrote:



On Apr 18, 8:19 am, "comp.llang.perl.moderated" <c...@blv-

sam-01.ca.boeing.com> wrote:
Another way if redirecting all -- rather than just
the ensuing output from the point of the interrupt --
to a file is acceptable:

open( my $fh, "$command |") or die "fork failed: $!" ;

local $SIG{ ALRM } = sub { close $fh;
system("$command >save.txt &"); exit;};

alarm $startupWait;
...
print while <$fh>; # eg.

I'm confused by this example, it seems that it would run the command
twice, discarding all the output the first time, and saving all the
output the second time. Definitely that's not what I'm looking for.

Did you try it... No, it shouldn't. The program will
print all output to the screen in real time. If, however,
your timeout occurs before program completion, then
the program is launched in the background and output re-directed as
you specified. I also assume that your
code included what you demo'ed in your post earlier:
alarm 0;
print "Command completed, output is:\n";
...

My main challenge was that I wanted to see that output in realtime,
but the command writes unbuffered output to STDOUT, and is prone to
hanging.

Again, if the program hangs and there's a timeout, then the handler
closes the pipe and launchs the program in the
background before exiting itself. And the pipe open is actually a
fork behind the scenes so the program is
running in a separate child process and can't pre-empt
a timeout handler in the parent.


If you're concerned about the timed out program running to completion
after
the background child program starts, be aware the child process will
terminate with a SIGPIPE as soon as it tries to write to the closed
pipe
in any event.

However, you might be able to force an even early termination:

my $child = open( my $fh, "$command |") or die "fork failed: $!" ;
local $SIG{ ALRM } = sub { close $fh; kill 'TERM', $child or kill
'KILL',$child;
system("$command
save.txt &"); exit;};
....

--
Charles DeRykus

.



Relevant Pages

  • Re: yet another os.popen quesion
    ... > function was only availible for UNIX, for which I am not currently ... is just to close your end of the pipe. ... until the child process does terminate. ...
    (comp.lang.python)
  • Re: Pipes on Wince (I have them), and device auto-loading.
    ... appropriate paths for each child process. ... the device nature of the pipe from the user, ... like to somehow have the device auto deactivate when the last ... In PipeLib there is a thread that waits ...
    (microsoft.public.windowsce.embedded)
  • Re: Strange Windows warning with IO.popen: The process tried to write to a nonexistent pipe
    ... premature termination of them (in the child process spawn by ruby). ... how do we fix it Luis? ... The process tried to write to a nonexistent pipe. ...
    (comp.lang.ruby)
  • Re: Parent process unable to read messages from child process
    ... exactly as you've posted here, However, the parent process populates ... the @detail array with only one pipe output. ... If the amount of output of each child process is small, ... close $uncompwrite; ...
    (comp.lang.perl.misc)
  • Re: does IO.read block?
    ... concurrency right: you need to make sure that stderr and stdout are ... on one of them fills up your child process is blocked. ... exception to chuck down the pipe. ... altered or corrupted during transmission. ...
    (comp.lang.ruby)