Perl terminate command on handle close
command, perl
Solution
`close` on this type of handle will start by closing the pipe, then it waits for the child to exit. The child will die from a SIGPIPE the next time it tries to write to the pipe. If you don't want to wait that long, you can speed things along and terminate it yourself.
my $pid = open ...;
while (<IN>) {
if (/Output/) {
# work is done
kill TERM => $pid;
last;
}
print; # Or whatever
}
close IN;
(Got rid of `$stop` since you weren't using it.)
Problem
I have the following code which reads output from the command 'some_command'. The 'some_command' is an ongoing process which keeps printing output until it is killed. I am wondering if I close the file handle or call on last, will it also terminate the 'some_command' process or it's possible that 'some_command' will keep running as an orphan process. I basically want to read output from the command until I find the line with /Output/ then terminates. ``` my $mycommand = 'some_command'; open IN, "$mycommand 123456 |" or die("Failed: $!"); while(my $line = <IN> && !$stop) { if($line =~ /Output/) { # work is done last; $stop = 1; } } close IN; ```