Thanks for the suggestion Luke, I think I got it now.

I'm a bit surprised. I don't do anything special regarding the zombie issue but top does not list any zombies - although the documentation warned about this and the parent process has already died? (I executed `./forker.pl find /usr -type f >/dev/null`.)

code
#!/usr/bin/env perl use strict; use warnings; use 5.010_001; # essential libraries use English qw( -no_match_vars ); use POSIX qw(:sys_wait_h); my @cmd = @ARGV; my $cmd_str = join ' ', @cmd; my $pid = fork(); if (not defined $pid) { # fork failed die "Cannot fork: $ERRNO\n"; } elsif ($pid == 0) { # child process say("Executing $cmd_str"); my $retval = system(@cmd); my $exitcode = parse_retval($retval); # TODO add URL callback say("[child] Exiting with exit code $exitcode."); exit $exitcode; } else { # parent process # wait a bit to give child process a chance to die prematurely sleep 1; my $cstatus = waitpid($pid, WNOHANG); my $exitcode; if ($cstatus == 0) { $exitcode = 0; say("[parent] Child is still running."); } else { $exitcode = parse_retval(${^CHILD_ERROR_NATIVE}); } say("[parent] Exiting with exit code $exitcode."); exit $exitcode; } sub parse_retval { my $retval = shift; my $signal = $retval & 127; my $have_coredump = $retval & 128; my $exitcode; if ($retval == -1) { # program failed to execute $exitcode = 127; } elsif ($signal) { my $message = "W: Process died with signal $signal!"; if ($have_coredump) { $message .= ' (coredump available)'; } warn($message ."\n"); $exitcode = 255; } else { $exitcode = $retval >> 8; } return $exitcode; }
result
$ ./forker.pl true ; echo "EXIT $?" Executing true [child] Exiting with exit code 0. [parent] Exiting with exit code 0. EXIT 0 $ ./forker.pl false ; echo "EXIT $?" Executing false [child] Exiting with exit code 1. [parent] Exiting with exit code 1. EXIT 1 $ ./forker.pl sleep 10 ; echo "EXIT $?" Executing sleep 10 [parent] Child is still running. [parent] Exiting with exit code 0. EXIT 0 <10 seconds later> [child] Exiting with exit code 0. $ ./forker.pl bogus ; echo "EXIT $?" Executing bogus Can't exec "bogus": No such file or directory at ./forker.pl line 22. [child] Exiting with exit code 127. [parent] Exiting with exit code 127. EXIT 127 $ ./bogus ; echo "EXIT $?" bash: ./bogus: No such file or directory EXIT 127

In reply to Re^2: fork, but not waiting on children by Monk::Thomas
in thread <solved> fork, but not waiting on children by Monk::Thomas

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.