I'm trying to figure out how to kill a child process after a timeout has occurred, where the child runs a Java app.

I have a program script.pl, that forks a child which calls 'exec' on a shellscript.sh which ends with 'exec $JAVA_BIN $OPT class "${1+$@}"'. The 'exec' call is supposed to replace the shell of the child so that it can be killed from the parent process after a timeout. However, the java process seems to disconnect itself from the execution chain and is not killed.

When I run the script.pl, the process tree looks like:

script.pl(27177)---sh(27180)---java(27181)-+-{java}(27186)
                                       |-{java}(27187)
                                       |-{java}(27188)
                                       `-{java}(27271)

When I run shellscript.sh directly, the process tree looks like:

java(26855)-+-{java}(26860)
            |-{java}(26861)
            |-{java}(26864)
            `-{java}(26871)

The child executes the process this way...

my @cmd = ($SCRIPT,'arg1');
exec(@cmd) or croak "Unable to execute @cmd: $!";

How can I call 'exec' on the script so if killed, the java app is killed?

Is the only way to make this work, calling the java app directly from the perl exec call?

Thanks Monks!


In reply to Calling exec on exec by shamu

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.