Clownburner has asked for the wisdom of the Perl Monks concerning the following question:

I have to figure out a way to have a web user initiate a process that will detach from Apache so the user doesn't have to wait for it - it could easily take 45 minutes to run. I'll have a page where they can come back later to get the results.

Unfortunately, my meager skills can't figure out a way to get a CGI process to 'spin off' from Apache so it can continue to run in the background. I know this is something a lot of people do, but I can't find anything in a search.

All help, guidance, hints, or suggestions gratefully appreciated.


"Non sequitur. Your facts are un-coordinated." - Nomad
  • Comment on Background processes triggered from Apache...

Replies are listed 'Best First'.
Re: Background processes triggered from Apache...
by dws (Chancellor) on Jul 04, 2002 at 00:31 UTC
    merlyn has a two part column that covers the basics for forking off a process from your CGI, and then redirecting the browser to the forked process, which runs is using HTTP::Daemon to be its own web server (on a different port).

    If you control the server, this if fair game. Some ISPs, however, will get miffed if they catch you doing this.

Re: Background processes triggered from Apache...
by grantm (Parson) on Jul 04, 2002 at 11:27 UTC

    I've had success with something as simple as:

      system("/path/to/command >logfile 2>&1 &")

    The trailing ampersand causes the process to go into the background and the system() function returns immediately. Later requests from the user can examine the contents of the logfile.

    I believe mod_perl does some magic to ensure that the background process does not inherit a copy of the socket that the request came in on (if it did, the initial request would not complete until the background process exited.

    If you're trying to do this on Windows, you might want to look at Win32::Process - I've used it for a similar effect.