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

robiticus

okay, here's another example, with no ampersand. In this case, the app is (apparently) running in the background, because my script continues (prints out "pid: 1234"). HOWEVER, I can't exit the script until I exit the java app.

#!/usr/bin/perl my($JAVA_CLASSPATH) = @ARGV; my $kid = open(my $fh, "java -classpath $JAVA_CLASSPATH TextEntry 2> / +dev/null |") or die "Couldn't launch 'launch_java.sh': $! / $?"; END { if ($kid) { kill 9 => $kid; # bang }; }; print "pid: $kid\n\npress return to exit script...\n\n"; <STDIN>;

Replies are listed 'Best First'.
Re^5: how to kill background process when script exit?
by roboticus (Chancellor) on Feb 19, 2011 at 23:44 UTC

    Alasso:

    Hmmm ... I'm playing with it to see what I can determine. In about 10 minutes, I've had no luck. I'll report back if I find something.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

    Update: Well, I've tried a couple things, but I've not found anything useful.

Re^5: how to kill background process when script exit?
by Corion (Patriarch) on Feb 20, 2011 at 00:14 UTC

    I'm confused now. I thought you wanted to kill the child program at the end of your script?

    Now you say you don't want to kill the child program. Which is it?

      I want to kill the child program when the perl script exits. not sure where you got the other.

        Then the code I gave you should already work and kill the child at the end of the program. Maybe closing the filehandle to the kid and also adding a call to kill outside the END block helps to kill the kid earlier.