Re: Close a Running Sub-Process
- From: xhoster@xxxxxxxxx
- Date: 30 Aug 2006 22:05:43 GMT
"mumebuhi" <mumebuhi@xxxxxxxxx> wrote:
I have a problem closing a filehandle, which is a pipe to a forked
process. The forked process basically tails a log file in another
server. I need to stop the child process once a particular line is
found.
The code is as the following:
This not the code. Please post real code.
# start code
my $fh = undef;
No need to predeclare it.
my $child_process = "ssh username@host tail --follow=name
file_to_be_tailed"
@host would be interpolated. You need a pipe character at the end
of your string for the open to do what you want. The lack of a trailing
semicolon creates a syntax error.
open $fh, $child_process || Carp::confess("can't open $child_process:
$!");
you have a precedence problem with the ||, it should be "or".
while (<$fh>) {
chomp;
if (/successful/) {
last;
}
}
close $fh;
# end code
The script will block when it tries to close the filehandle. How do I
force it to close while tail is still running?
You capture the pid of the running process (it is the return value of a
pipe open), and then you kill it just prior to the close.
my $pid=open my $fh, $cmd or die $!;
#....
kill 1,$pid;
close $fh;
You can use 2 or 15 instead of 1 to kill it with, but 1 seems to do the job
with generating spurious messages to STDERR on my system. You can't use 13
(SIGPIPE) because if the child honored that signal, you wouldn't have the
problem in the first place.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.
- Follow-Ups:
- Re: Close a Running Sub-Process
- From: xhoster
- Re: Close a Running Sub-Process
- References:
- Close a Running Sub-Process
- From: mumebuhi
- Close a Running Sub-Process
- Prev by Date: Re: Threading - share
- Next by Date: Re: Close a Running Sub-Process
- Previous by thread: Re: Close a Running Sub-Process
- Next by thread: Re: Close a Running Sub-Process
- Index(es):
Relevant Pages
|