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

REALLY kludgy, but it works :-)...

#!/usr/bin/perl my($JAVA_CLASSPATH) = @ARGV; $java_command = "java -classpath $JAVA_CLASSPATH TextEntry"; system("$java_command 2> /dev/null &"); $kid = `ps aux | grep "$java_command" | grep -v 'grep'`; $kid =~ s@^.*?\s+([0-9]+).*@$1@s; END { if ($kid) { kill 9 => $kid; # bang }; }; print "pid is: $pid\n\npress return to exit..."; <STDIN>

I would really like to find a more elegant way however.

Replies are listed 'Best First'.
Re^3: how to kill background process when script exit?
by eyepopslikeamosquito (Archbishop) on Feb 20, 2011 at 00:23 UTC

    You could use fork and exec instead. For example (no Unix box right now, so untested) something like:

    use strict; my $JAVA_CLASSPATH = '...whatever'; my $java_command = "java -classpath $JAVA_CLASSPATH TextEntry"; my $kid; defined($kid = fork()) or die "error: fork: $!"; if ($kid == 0) { ## child # Could redirect child stdout/stderr here if desired. # open(STDOUT, '>', 'javastdout.tmp') or die "redirect stdout: $ +!"; # open(STDERR, '>', 'javastderr.tmp') or die "redirect stderr: $ +!"; exec($java_command); die "error: exec: $!"; } warn "parent continues ... child pid is $kid\n"; # Could wait for child to exit using waitpid if required. # waitpid($kid, 0); # script continues... END { # Update: should first check that $kid is still alive before killi +ng it. # Something like: kill 0, $kid should do it (see "perldoc -f kill" +) if ($kid) { kill 9 => $kid; # bang }; };
    Also, as a matter of technique, you should try "kill 15" (SIGTERM) before resorting to "kill 9" so as to give the Java process a chance to catch the SIGTERM signal, clean up and die more gracefully.

    Update: For more elaborate sample code, using fork/exec, "kill 15" (softer kill before resorting to "kill 9"), "kill 0" (to check if process is still alive), and waitpid (to reap the exited child and test its exit code), see Timing and timing out Unix commands (especially the "kill_it" subroutine).

      Abbot:

      Just saw that, I'll have to look into it tomorrow, thanks.

      eyepopslikeamosquito:

      thanks, I had a chance to go through it, and it works fine. I was unfamiliar with fork() and exec() so I had to do a bit of studying first. I think open[_23]() provide a few more options and are more straightforward though, but maybe there is a price for this. Can you tell me any advantages to using fork()/exec() over open()?

      All in all, the experience has been very good for me. The whole thing about processes and threads has always been kind of mysterious to me, and I never have had a reason to grind through and get it figured out.

      I like your kill_it routine, and will use it.
Re^3: how to kill background process when script exit?
by Allasso (Monk) on Feb 20, 2011 at 00:23 UTC

    okay, this works (not sure if it is much more elegant, but...)

    perl script:

    #!/usr/bin/perl my($JAVA_CLASSPATH) = @ARGV; $LAUNCH_JAVA = $JAVA_CLASSPATH."/launch_java.sh"; open(FH, "$LAUNCH_JAVA $JAVA_CLASSPATH |") or die "Couldn't launch 'launch_java.sh': $! / $?"; my $kid = <FH>; $kid =~ s@\n@@; END { if ($kid) { kill 9 => $kid; # bang }; }; print "pid: $kid\n\npress return to exit script...\n\n"; <STDIN>;

    shell script (launch_java.sh):

    #/bin/bash java -classpath $1 TextEntry 2> /dev/null & echo $!