advait has asked for the wisdom of the Perl Monks concerning the following question:

Hi All
I am running a cgi script which calls another perl script which takes long time to run.I am running this time consuming script in the background. Now the problem is the cgi scripts keeps on running in the browser till the background script runs. I want to kill this parent cgi script and let the background script running
#!/usr/bin/perl -w use CGI; use strict; my $query = new CGI; my $DirName =$query->param("dir"); my $maxSeqLen=$query->param("maxSeqLen"); my $minSeqLen=$query->param("minSeqLen"); my $WindowSize=$query->param("WindowSize"); my $Email=$query->param("email"); print $query->header(); my $cmd=" /var/www/cgi-bin/nuScore/ProcessLongData.pl $DirName $maxSeq +Len $minSeqLen $WindowSize $Email &"; system($cmd); print "<html>\n"; print "<head>\n"; print "<title>nuScore</title>\n"; print "<body>\n"; print "<table width='641' border='0' cellspacing='0' cellpadding='0' a +lign='left'>\n"; print "<tr> <td>Thank you for submiting your email address. Sequences +will be processed and the link to results page will be sent by email. +<br>< </td> </tr>\n"; print "<hr></table></body></html>\n";

Replies are listed 'Best First'.
Re: How can I kill pid of parent and keep that of child alive
by pc88mxer (Vicar) on Jun 06, 2008 at 18:50 UTC
    Most likely you just need to close(STDOUT) in the child process. See this node: CGI and Background Processes.

    In your case I would do this:

    use IPC::Open2; my @cmd = ('/var/www/cgi-bin/nuScore/ProcessLongData.pl', $DirName, $maxSeqLen, $minSeqLen, $WindowSize, $Email); open(NULL, "+</dev/null"); my $pid = open2(">&NULL", "<&NULL", @cmd); ...
    This is much, much safer since the parameters for your script, $DirName, $maxSeqLen, etc. are coming directly from your CGI parameters.
Re: How can I kill pid of parent and keep that of child alive
by kyle (Abbot) on Jun 06, 2008 at 18:53 UTC
Re: How can I kill pid of parent and keep that of child alive
by graff (Chancellor) on Jun 07, 2008 at 02:17 UTC
    Maybe you are running this on a machine that is not really accessible to "untrusted" clients? I certainly hope so, because for any web script that is generally accessible, interpolating cgi params directly into a string that is being interpreted by a shell is asking for trouble. Even for limited-access jobs, this is something that really should be avoided -- at least do some sanity checks on the params first.

    Apart from that, even if this is only accessible to "trusted" clients, there might be an issue about how many clients might hit the script at a given time (or how many times one client might hit it in rapid succession), causing lots of long, heavy processes to be running concurrently on the server. Good luck with that.