I tried something along the lines of #1 and #2 and it still causes an Apache2 child segmentation fault after a few successful requests. Here is the code I used:

sub RunCommand { my $pgm = shift; my @execargs = @_; $SIG{CHLD} = 'IGNORE'; # This should flush stdout. my $ofh = select(STDOUT);$| = 1;select $ofh; warn "Before first fork $$"; my $kpid = fork; defined($kpid) || die; if ($kpid) { # Parent process my $w = waitpid($kpid, 0); # warn "waitpid returned $w\n"; } else { close STDIN; close STDOUT; close STDERR; setsid() >= 0 or die; my $gpid = fork; defined($gpid) or die; if ($gpid) { my $w = waitpid($gpid, 0); # warn "waitpid returned $w\n"; } else { open(STDIN, "</dev/null") or die; open(STDOUT, ">/dev/null") or die; open(STDERR, ">/dev/null") or die; # Child process exec($pgm, @execargs) or die; } CORE::exit(0); } }

I invoked that function from within my mod_perl2 response handler as:

&RunCommand("/home/dbooth/rdf-pipeline/trunk/pid.perl");

The pid.perl command I ran uses no stdin, stdout or stderr, but merely logs its PID and date to /tmp/pid.txt:

#! /usr/bin/perl -w # Append the PID and date/time to /tmp/pid.txt # so that we can verify that this ran. use HTTP::Date; my $tmp = "/tmp/pid.txt"; open(my $fh, ">>$tmp") or die; my $t = time2str(time); print $fh "pid: $$ $t\n"; close $fh or die; exit 0;

P.S. See also the update to my original post, in which I note that this command still runs successfully during the HTTP request in which the Apache2 child process dies from a segmentation fault.


In reply to Re^2: Correct way to run a shell command from mod_perl2? (Child process segmentation fault) by dbooth
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.