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

Hi Monks, I've got a Perl script, lets call it job.pl, running in response to an HTTP request running under Apache. The script has 2 jobs to do:
1) respond to the user,
2) a potentially much longer job

I want to run the longer job in the background, so the browser user doesn't get bored and can also close his browser. I've tried backticks and system to initiate the longer job e.g. as follows

job.pl contains...... `path\longerjob.pl &`; print "response to user"; exit;
but the whole response waits for longerjob to complete. Perhaps this is really a Unix question but "path\longerjob.pl &" works fine on the Unix command line. Why does the perl script wait for it to complete ? Can I stop Perl waiting ? I haven't tried fork yet is that an answer ? the only answer ?
Thanks.

Replies are listed 'Best First'.
Re: A background job from a browser
by marto (Cardinal) on Jan 28, 2010 at 14:19 UTC
      Thanks Merlin
      If I've understood that correctly the browser can exit and the child will continue happily undisturbed,
      which is exactly what I want.

      Much appreciated.
      BRgds
        Thanks Merlyn (not Merlin !)
Re: A background job from a browser
by Fox (Pilgrim) on Jan 28, 2010 at 14:33 UTC
    maybe it's waiting for output

    try
    `path\longerjob.pl >/dev/null 2>&1 &`;

    to throw away the output
Re: A background job from a browser
by Anonymous Monk on Jan 28, 2010 at 17:54 UTC
    The process must let go of STDOUT for longerjob() to complete and the page to display. I believe the simplest way to do this would be a fork(), try open2() or open3() as well. But you may be able to get something to chug along in the same process you started with if you close STDOUT before executing the process. Does this help ?
      Thanks for your suggestion. I'll give this a try and update once I know if it helps.

      BRgds