Link to home
Start Free TrialLog in
Avatar of shahrahulb
shahrahulb

asked on

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.

SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shahrahulb
shahrahulb

ASKER

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

Rahul

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';
               exit;
}
die "Can't fork $!" unless defined $p;
When I have had this I have just:
close(STDOUT);
and the web server has continued.