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

Alasso:

I bet it's those ampersands that are getting in your way. The open statement, when given a string ending with a vertical bar tells perl to try to run the program in the background and redirect the standard output to the opened file stream. So in the first case, it fails because the string didn't end with a vertical bar, so it tried to use it as a filename. The second and third fail because when the shell starts, it then tries to open another process in the background. So you lose control of the child of the child.

Give it a go without the ampersand, and let us know if that doesn't fix it.

...roboticus

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

  • Comment on Re^3: how to kill background process when script exit?

Replies are listed 'Best First'.
Re^4: how to kill background process when script exit?
by Allasso (Monk) on Feb 19, 2011 at 23:20 UTC

    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>;

      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.

      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.