in reply to fork/exe, system open etc.

One sometimes overlooked detail is that you need to close the STDOUT/STDERR handles in the child process for the parent to continue and finish its job normally. (Or better yet, reopen them to some logfile...)

Replies are listed 'Best First'.
Re^2: fork/exe, system open etc.
by pc88mxer (Vicar) on May 02, 2008 at 19:28 UTC
    I think this cuts to the heart of the matter. It's not that your parent is waiting for the child to finish, but that the Apache server is waiting for the output pipe from the CGI script to close, and the child process still has it open. Closing STDOUT in the child should fix the problem. Try one of these:
    system("perl script.pl >/dev/null &"); open(CMD, "|perl script.pl > /dev/null");

    To illustrate that both the system and open methods will spawn a child process without blocking the parent, both of these scripts will continue to print "boo" even after the parent has exited:

    system("while true; do echo boo; sleep 2; done &"); print "system done - exiting\n";
    open(CMD, "|while true; do echo boo; sleep 2; done"); print "open returned - exiting\n";
      It's not that your parent is waiting for the child to finish, but that the Apache server is waiting...

      You're absolutely right, I should've said grandparent (from the child's perspective)... Of course it's the Apache process which hangs waiting for the long-running process to close the pipes that were duplicated by the fork.

      Brilliant, and yet so simple. It makes me wonder why it's not discussed in any of the documentation i've read (unless, of course, i'm reading the wrong documentation). Thanks pc88mxer.