perl cgi background process

Posted on 2005-04-09
Medium Priority
Last Modified: 2012-06-22
i have a cgi script. problem is in that cgi script i m updating database...this takes approximately 2 minutes.
so the user has to wait 2 minutes when the browser is open which does not maks any sense.

can i do something like create a perl script (say background.pl) which does the updation of database.
when the user opens the cgi, the cgi should invoke the perl script but at the same time this script should be running in the background...so that the user in the foreground sees that everything is done...only thing is the perl application is running in background so that user does have to wait for 2 minutes to receive confoirmation...

as soon as user hits the submit button, he should immidiately get the confirmation

is it possible to do in cgi script.

Question by:shahrahulb
  • 2
  • 2
  • 2
LVL 85

Assisted Solution

ozo earned 600 total points
ID: 13745938
system('background.pl &');

Accepted Solution

jhurst earned 1400 total points
ID: 13746060
I do not believe that the above will be sufficient since in most systems the web server will not send the output till it knows that it is all there.  So, I believe that you need to close STDOUT.

Additionally, I would actually use a fork() rather than a system since this is cleaner and should be alot faster.

Author Comment

ID: 13746808
ozo, i have already tried  system('background.pl &');  but web server waits untill it finishes the execution of background.pl before executing the next statement.

as jhurst suggested, how do we close STDOUT


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 13746828
thx jhurst....i got it.
close(STDOUT) just before executing the system command
LVL 85

Expert Comment

ID: 13746844
If you are running on MSwindows, system('cmd &') or fork may not work.
If you're running on Unix
close STDOUT; system('background.pl &');
could work.
or you could daemonise the child with
my $p=fork();
if( $p==0 ){
               chdir '/'               or die "Can't chdir to /: $!";
               open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
               open STDOUT, '>/dev/null'
                                       or die "Can't write to /dev/null: $!";
               defined(my $pid = fork) or die "Can't fork: $!";
               exit if $pid;
               setsid                  or die "Can't start a new session: $!";
               open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
               do 'background.pl';
die "Can't fork $!" unless defined $p;

Expert Comment

ID: 13748274
When I have had this I have just:
and the web server has continued.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question