Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Detached forking in a CGI script

by flexvault (Monsignor)
on Apr 29, 2016 at 18:22 UTC ( [id://1161897] : note . print w/replies, xml ) Need Help??

in reply to Detached forking in a CGI script


    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.

A few years back and this process was easy, but today many of the web servers will not allow children to continue to run if the parent exits. I've gotten around this by using a continuous running Perl script that keeps checking a file for instructions on work to be passed to new forked script.

The cgi script locks the file, adds the work to be done to that file, and then closes (which unlocks also) the file and exits.

The continuous running script sees the new work and forks to do the required work. (Note: You can also use 'pre-forked' scripts to do this.)

Possibly the referenced module does something similar and may give a more complete solution than using your own 'fork'. Just because it uses 'fork' and 'exec' doesn't mean that there isn't a whole lot more logic necessary to provide independent processes to complete the work after the parent cgi script exits. YMMV


"Well done is better than well said." - Benjamin Franklin