• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 707
  • Last Modified:

perl cgi background process

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.

  • 2
  • 2
  • 2
2 Solutions
system('background.pl &');
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.
shahrahulbAuthor Commented:
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


Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

shahrahulbAuthor Commented:
thx jhurst....i got it.
close(STDOUT) just before executing the system command
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;
When I have had this I have just:
and the web server has continued.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now