Re: How to time out a forked command but still see output?
- From: "comp.llang.perl.moderated" <ced@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: 21 Apr 2007 23:36:06 -0700
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
.
- References:
- How to time out a forked command but still see output?
- From: thecrow
- Re: How to time out a forked command but still see output?
- From: comp.llang.perl.moderated
- Re: How to time out a forked command but still see output?
- From: thecrow
- Re: How to time out a forked command but still see output?
- From: comp.llang.perl.moderated
- How to time out a forked command but still see output?
- Prev by Date: Re: How to time out a forked command but still see output?
- Next by Date: FAQ 4.28 How do I change the Nth occurrence of something?
- Previous by thread: Re: How to time out a forked command but still see output?
- Next by thread: Socket creation failing with "operation now in progress" error
- Index(es):
Relevant Pages
|