Looking at the "Complete Fork Example" (OP bullet 1) outlined in Practical mod_perl, it reminds me of code to daemonize a forked process. You know, set a new session, be a process group leader, close file descriptors, chdir /, yada yada Richard Stevens yada, etc. Perhaps that will cut the umbilical cord of the child from the main Apache process.

Try this example:
my $pid = open(my $FROM_KID, "-|"); die("Failed fork: $!") unless defined($pid); if ($pid) # parent { chomp(my $output = do { local $/; <$FROM_KID> }); waitpid($pid, 0); printf <<'RESULT', $output, $? >> 8; Child output: %s Exit: %d RESULT } else { daemonize_self("", undef); # leave stdout alone exec "date" or die("Failed exec: $!"); }

Now for the code of daemonize_self():
use Cwd (); use POSIX (); use File::Spec; sub daemonize_self { my ($stdout_log, $stderr_log) = @_; # new session & process group leader, and no controlling tty die("daemonize_self: Cannot detach from controlling tty") if POSIX::setsid() < 0; # ignore signals for my $s (qw(INT HUP TTIN TTOU TSTP)) { $SIG{$s} = sub { }; # not "IGNORE", not inherited } $SIG{CHLD} = "IGNORE"; # inherited default to reap kids $SIG{TERM} = "DEFAULT"; # be lenient: redirect stdin, stdout, stderr my $DEVNULL = File::Spec->devnull(); open(*STDIN, "<", $DEVNULL) or die("daemonize_self: Cannot silence STDIN", "\n ", $!); unless (defined($stdout_log) && $stdout_log eq "") { open(*STDOUT, ">>", defined($stdout_log) ? $stdout_log : $DEVN +ULL) or die("daemonize_self: Cannot redirect STDOUT", "\n ", $!) +; } unless (defined($stderr_log) && $stderr_log eq "") { open(*STDERR, ">>", defined($stderr_log) ? $stderr_log : $DEVN +ULL) or die("daemonize_self: Cannot redirect STDERR", "\n ", $!) +; } # close all other file descriptors POSIX::close($_) for 3 .. 1024; # reset file creation mask umask 0; # change current working dir to root chdir(File::Spec->rootdir()); }

In reply to Re: Correct way to run a shell command from mod_perl2? (Child process segmentation fault) by repellent
in thread Correct way to run a shell command from mod_perl2? (Child process segmentation fault) -- SOLVED! by dbooth

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.