Re: Close a Running Sub-Process



"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
.



Relevant Pages

  • Re: killing child processes
    ... >> How can I get one child process to kill another child process in ksh? ... UID PID PPID TTY STIME COMMAND ...
    (comp.unix.shell)
  • Re: Can Python kill a child process that keeps on running?
    ... Does Python have any way to kill it? ... Also take a look at buildbot sources ... Note, however, that ctypes is planned to be a part of the 2.5 distribution, so while there may not be a platform-independent way to achieve your goals you will at leats be able to do so without external extensions. ... Can you explain to me how ctypes will let me kill a child process? ...
    (comp.lang.python)
  • Re: How to kill child & parent process simulanteously
    ... > Now i am interested to stop the child process when the parent dies. ... > in that case i want to kill the child of the process. ... You should start a process group at the parent process so the child be ... in ps) then it won't disappear anyway until this I/O terminates ...
    (comp.unix.programmer)
  • Net::Telnet, fork and signals..
    ... the SIGINT is sent on the wire to the remote command being run. ... kill 'INT' $pid; ... see that command being executed on host-A (by listing the processes ...
    (comp.lang.perl.modules)
  • Re: PID of a process
    ... process A should not kill that process. ... To be more precise, systemblocks, waiting for the child process to complete. ... Unless you launch the child process as system, there is no PID to ...
    (comp.os.linux.development.system)