I found that redirecting output causes the process to be run in a shell (this is un-explicitly mentioned in some or all of the open[ 23]() docs), thus the pid returned is the pid of the shell process, not my command.

ps aux | grep java | grep -v grep

Revealed that it was killing the shell, but killing the shell did not stop the process from running.

Corion was correct.

ikegami tried to tell me about the shell thing, but I didn't catch what he was saying.

here are some examples using open(), open2() and open3() and the results I observed:

use IPC::Open2 qw( open2 ); use IPC::Open3 qw( open3 ); # this works: my $kid = open(FH1, "java -classpath $PATH TextEntry"); my $kid = open2(FH1, FH2, "java -classpath $PATH TextEntry"); my $kid = open3(FH1, FH2, FH3, "java -classpath $PATH TextEntry"); # however, when I try to suppress stderr, # it doesn't work because of the redirection, # and the process is launched it in a shell; # $kid is the pid of the shell: my $kid = open(FH1, "java -classpath $PATH TextEntry 2> /dev/null"); my $kid = open2(FH1, FH2, "java -classpath $PATH TextEntry 2> /dev/null"); # no problem, open3() will capture STDERR, taking # care of the redirection: my $kid = open3(FH1, FH2, FH3, "java -classpath $PATH TextEntry"); # however, the use IPC::Open2() docs # (to which IPC::Open3() is compared to) # indicates that the above form would be run in a shell, # so it may be safer to do this: my $kid = open3(FH1, FH2, FH3, "java", "-classpath", "$PATH", "TextEntry"); # but there is a way to capture the child output # using open() and still kill it when parent exits: # exec the command from the shell, thus # $kid = pid of shell = pid of the command # suggested by ikegami: my $kid = open(FH1, "exec java -classpath $PATH TextEntry 2> /dev/null");

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.