Long running CGI script

S

Stephen O'D

I have a script that asks for some input on several screens, and at the
end it kicks off a connection to a database which does some work that
could potentially take a long time. I dont want to hold up displaying
the webpage while this is running - I would prefer to print a message
to the browser saying the job is running in the background. In other
works, I want to allow the script to continue running, but notify the
browser that I have finished talking to it.

I found a script written by Randal Schwartz that says todo this all you
have todo is close STDIN and STDOUT:-

open STDIN, "</dev/null";
open STDOUT, ">/dev/null";

However when I do this the script seem to die (although no die message
gets into the error logs - it just seems to give up silently). Also, a
system command to copy a file gives an error "Not A Typewriter". If I
remove the system command, the database connection does not work
correctly (it seems to work sometimes though??!!).

How can I 'disconnnect' the browser let my script continue todo some
work? I would prefer not to fork.

Thanks,

Stephen.
 
L

Larry

I always thought you had to "fork" to do that. In addition you need to
close STDOUT (as you've done) before you fork.

As for the dying, you need to find out where it's hapenning. You
mention that you have a log file. Make sure you do $| = 1 after
opening the log file, and just print progress messages to the log file
every few lines in your code. Eventually you'll find the exact place
where it's dying, and you'll be able to debug it better.
 
X

xhoster

Stephen O'D said:
I have a script that asks for some input on several screens, and at the
end it kicks off a connection to a database which does some work that
could potentially take a long time. I dont want to hold up displaying
the webpage while this is running - I would prefer to print a message
to the browser saying the job is running in the background. In other
works, I want to allow the script to continue running, but notify the
browser that I have finished talking to it.

I found a script written by Randal Schwartz that says todo this all you
have todo is close STDIN and STDOUT:-

open STDIN, "</dev/null";
open STDOUT, ">/dev/null";

However when I do this the script seem to die (although no die message
gets into the error logs - it just seems to give up silently).

Apache, and probably other web servers, will kill the CGI process once
the process closes STDOUT and STDERR. That is probably why your script
is dying. Either reconfigure the server not to do this (I don't know how
to do that) or fork a new process to do the heavy lifting so that the
original process can go away gracefully.

Also, a
system command to copy a file gives an error "Not A Typewriter". If I
remove the system command, the database connection does not work
correctly (it seems to work sometimes though??!!).

Sorry, I can't make sense of this. Can you show the relevant code?

How can I 'disconnnect' the browser let my script continue todo some
work? I would prefer not to fork.

I'd prefer to stop aging, but that isn't working out for me, either :)

Xho
 
K

kevindotcar

Are you on a win32 machine? If so, Why can't you just code

print "this may take awile...";
system("myscript.pl");

I'm not really a Unix Perl guy, but "shell()" sounds familiar.

[---]
Apache, and probably other web servers, will kill the CGI process once
the process closes STDOUT and STDERR. That is probably why your script
is dying. Either reconfigure the server not to do this (I don't know how
to do that) or fork a new process to do the heavy lifting so that the
original process can go away gracefully.
[---]

Try here:
http://perldoc.perl.org/File/Copy.html

Try looking at this page;
http://perldoc.perl.org/functions/exec.html

Exec() and system() seem appropos to your needs- but maybe I'm
misreading you.

If what you need is a formal "callback" functionality, you might be
best served by using semaphores- as Perl isn't really an "event-driven"
infrastructure.


kDot
 

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,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top