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

First,

$kid = open (FH, "java JavaApp 2> /dev/null & |");

should be

$kid = open (FH, "java JavaApp 2> /dev/null |");

It makes no sense to use "&" there. Removing it might even solve your problem. $kid would still refer to the shell, but killing the shell might kill its children too. Killing the negative of the shell's PID has an even greater chance of working. But why keep the shell around?

$kid = open (FH, "exec java JavaApp 2> /dev/null |");

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

    ikegami,

    I agree that it makes no sense to use it there, I was trying stuff out of desparation :-). It was interesting however the results that it did have, even though not what I ultimately want.

    It seems the consensus is that using open() to launch the app should run it in the background, but it just does not seem to be what I experience. My code (in the perl script) does not continue to execute until the child process has quit.

      Sorry, but you are mistaken.
      $ perl -E' say "".localtime; open my $fh, q{perl -e sleep |}; say "".localtime; ' Sun Feb 20 13:25:05 2011 Sun Feb 20 13:25:05 2011 [hung waiting for child to exit]

      or how about

      $ perl -E' open my $fh, q{perl -E'\''sleep 1; say STDERR "C"'\'' |}; say "P"; ' P C
        interesting...I agree with you (see my last reply to Corion), but when I run your second script I get this (no fooling):
        perl -e ' open my $fh, q{perl -e '\''sleep 1; print STDERR "C"'\'' |}; print "P"; ' CP
        (I couldn't use 'say', you must have a later version of perl than I?)
Re^4: how to kill background process when script exit?
by Allasso (Monk) on Feb 21, 2011 at 18:04 UTC
    I just caught your third example there - and it works also:
    $kid = open (FH, "exec java JavaApp 2> /dev/null |");
    So then instead of the shell running the app, the shell becomes the app, so the pid is the same. clever.