CGI: Running a forked background process

H

Hal Vaughan

I have a web page (served up by Apache 2) where a user click a link to
download a file. The link calls my Perl CGI script which takes the
parameters and generates a file to download. It takes time to generate the
file, so my idea was to fork like this:

if (!fork()) {
system("$ifile person=$id location=$loc");
exit();
}

And while the forked program is generating the download file, another web
page pops up saying, "Please wait..." and includes a Javascript countdown
until the next time it'll check and see if the download is ready. That
way, when the user clicks a link, they don't have to wait and get a new
page immediately. The new page has a countdown to let them know they're
not forgotten.

I've found, though, that the fork is not working. When I click on the link,
I get a LONG pause and eventually the countdown page appears, but only
after the forked program is done with it's work.

How can I fork so the download file is generated in the background, without
delaying displaying the other page?

Thanks!

Hal
 
X

xhoster

Hal Vaughan said:
I have a web page (served up by Apache 2) where a user click a link to
download a file. The link calls my Perl CGI script which takes the
parameters and generates a file to download. It takes time to generate
the file, so my idea was to fork like this:
....

I've found, though, that the fork is not working. When I click on the
link, I get a LONG pause and eventually the countdown page appears, but
only after the forked program is done with it's work.

The child program is holding onto the STDOUT and STDERR that it inherited
from the parent. Apache knows that STDOUT is not yet closed, and therefore
assumes the CGI isn't done yet and holds the connection to the browser open
(or something like that.) The forked process needs to close or reopen
STDOUT/STDERR in order to cut the web browser free.

Xho
 
H

Hal Vaughan

The child program is holding onto the STDOUT and STDERR that it inherited
from the parent. Apache knows that STDOUT is not yet closed, and
therefore assumes the CGI isn't done yet and holds the connection to the
browser open
(or something like that.) The forked process needs to close or reopen
STDOUT/STDERR in order to cut the web browser free.

Xho

That did it. Thank you!

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top