perl cgi background process

Posted on 2005-04-09
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 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 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
    LVL 84

    Assisted Solution

    system(' &');
    LVL 8

    Accepted Solution

    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.
    LVL 1

    Author Comment

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

    as jhurst suggested, how do we close STDOUT


    LVL 1

    Author Comment

    thx jhurst....i got it.
    close(STDOUT) just before executing the system command
    LVL 84

    Expert Comment

    If you are running on MSwindows, system('cmd &') or fork may not work.
    If you're running on Unix
    close STDOUT; system(' &');
    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 '';
    die "Can't fork $!" unless defined $p;
    LVL 8

    Expert Comment

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

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    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…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now