in reply to Re^2: how to kill background process when script exit?
in thread how to kill background process when script exit?

I found that redirecting output causes the process to be run in a shell (this is un-explicitly mentioned in some or all of the open[ 23]() docs), thus the pid returned is the pid of the shell process, not my command.

ps aux | grep java | grep -v grep

Revealed that it was killing the shell, but killing the shell did not stop the process from running.

Corion was correct.

ikegami tried to tell me about the shell thing, but I didn't catch what he was saying.

here are some examples using open(), open2() and open3() and the results I observed:

use IPC::Open2 qw( open2 ); use IPC::Open3 qw( open3 ); # this works: my $kid = open(FH1, "java -classpath $PATH TextEntry"); my $kid = open2(FH1, FH2, "java -classpath $PATH TextEntry"); my $kid = open3(FH1, FH2, FH3, "java -classpath $PATH TextEntry"); # however, when I try to suppress stderr, # it doesn't work because of the redirection, # and the process is launched it in a shell; # $kid is the pid of the shell: my $kid = open(FH1, "java -classpath $PATH TextEntry 2> /dev/null"); my $kid = open2(FH1, FH2, "java -classpath $PATH TextEntry 2> /dev/null"); # no problem, open3() will capture STDERR, taking # care of the redirection: my $kid = open3(FH1, FH2, FH3, "java -classpath $PATH TextEntry"); # however, the use IPC::Open2() docs # (to which IPC::Open3() is compared to) # indicates that the above form would be run in a shell, # so it may be safer to do this: my $kid = open3(FH1, FH2, FH3, "java", "-classpath", "$PATH", "TextEntry"); # but there is a way to capture the child output # using open() and still kill it when parent exits: # exec the command from the shell, thus # $kid = pid of shell = pid of the command # suggested by ikegami: my $kid = open(FH1, "exec java -classpath $PATH TextEntry 2> /dev/null");