xylose has asked for the wisdom of the Perl Monks concerning the following question:
I'm having a problem satisfying a number of constraints in a CGI script I'm writing.
I need the script to fork a child process and to get hold of the pid of this child, but to then not make the parent (the CGI process) wait for the child, and allow it to exit immediately.
One extra complication is that in the child I exec a new program (an R script), but capture STDOUT and STDERR into log files, so I can't close those filehandles before doing the exec.
I've tried a number of different approaches, but anything which gives me the pid and starts the child process correctly always makes the parent hang until the child is done.
My current (not working) best guess is:
$SIG{CHLD} = 'IGNORE'; my $pid = fork(); if ($pid) { # We're the child and we need to start the analysis # First we'll write out pid into a file in the run # folder so that the results tracker can tell if we've # died. open (PID,'>','pid.txt') or die "Failed to write to pid file : $!" +; print PID $pid; close PID or die "Failed to write to pid file: $!"; exec("Rscript analysis.r > log.txt 2>errors.txt"); exit(0); } print $q->redirect("script.cgi?job_id=$job_id");
In this version the child runs, but the parent doesn't exit until the child has completed.
Any ideas how I can have my cake and eat it?
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Detached forking in a CGI script
by FreeBeerReekingMonk (Deacon) on Apr 29, 2016 at 20:39 UTC | |
|
Re: Detached forking in a CGI script
by stevieb (Canon) on Apr 29, 2016 at 15:34 UTC | |
by queldor (Acolyte) on Apr 29, 2016 at 17:49 UTC | |
by stevieb (Canon) on Apr 29, 2016 at 22:08 UTC | |
by queldor (Acolyte) on Apr 30, 2016 at 00:19 UTC | |
|
Re: Detached forking in a CGI script
by flexvault (Monsignor) on Apr 29, 2016 at 18:22 UTC | |
|
Re: Detached forking in a CGI script
by Anonymous Monk on Apr 30, 2016 at 00:21 UTC |