Perl script needs to receive ajax request, send a “success” message back to browser, and then “spawn” a process to run in the background. (this spawned process could take 10 minutes to run, so I don’t want the browser to wait for that – or for an apache timeout to occur)
I was under the impression that the Perl exec() function would do this. Better than using fork() or system() since those are supposed to wait for a response.
But, the script seems to be waiting for the exec() program to finish before sending the success message to the browser. Here’s the very end of my Perl script:
print "Content-type: text/html\n\n";
print "success"; # this gets returned via ajax
exec $script_filename, $var1, $var2, $var3;
Everything works, but the browser isn’t receiving the “success” message until $script_filename finishes running.
What I want to do seems similar to this previous post, but there aren’t enough specifics there.
Any ideas? Thanks.
I found an answer to another StackOverflow question which seems to solve my problem – see Paul Tomblin’s response here.
It seems that if the perl script does exec(), then apache waits. But if the perl script first does a fork, and then the fork does the exec(), then everything works as expected.
And here’s what it looks like in my code, replacing what was in my original post:
Thanks to Adam for confirming right away that what I was originally trying to do was flawed.
Thanks to Jonathan for pointing me in the right direction.