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

I want to kill the child program when the perl script exits. not sure where you got the other.
  • Comment on Re^6: how to kill background process when script exit?

Replies are listed 'Best First'.
Re^7: how to kill background process when script exit?
by Corion (Patriarch) on Feb 20, 2011 at 08:15 UTC

    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.

      Okay, I tried that - closing the filehandle and putting the kill at the end of the script - still no go. Perl program stops executing until I exit the java app. It just does not seem that open() is launching the process in the background.

        The following code behaves just like it should (and like I said) for me:

        corion@aliens:~/tmp$ cat test.pl #!perl -w use strict; my $pid = open my $fh, 'sleep 1000 |' or die "Couldn't spawn: $! / $?"; print "Spawned child as $pid"; print "Doing own work $_\n" for 1..10; print "Killing child $pid"; kill 9 => $pid; print "done\n";
        corion@aliens:~/tmp$ perl -w test.pl Spawned child as 26868Doing own work 1 Doing own work 2 Doing own work 3 Doing own work 4 Doing own work 5 Doing own work 6 Doing own work 7 Doing own work 8 Doing own work 9 Doing own work 10 Killing child 26868done

        I'm not sure what you're doing differently.