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

Hi All, I take the data from user and tell the user that result will be emailed.
On the server side I process the data . For processing I have made a small perl script which breaks the data into multiple small segments and process them in using system command
$command ="nice R --slave --args $DirName $SubDirName < read_parameter +s_by_file_batch.R "; system($command);

System calls R till the main cgi script is alive. I can see cgi as defunctional using top. Once cgi dies system command doesnt gets executed
Last few lines of main cgi script are
$cmd = "perl /var/www/cgi-bin/vcgi/ProcessFiles_test.pl $UploadDir + $DirName $TotFiles &"; system ($cmd); fork && exit;

Can anyone please suggest how to overcome this situation thank you

Replies are listed 'Best First'.
Re: system command not working after connection with cgi breaks
by pc88mxer (Vicar) on Jun 23, 2008 at 17:57 UTC
    Try these two things:

    1. Remove the fork && exit at the end of your script - you don't need it.

    2. On all of your system() commands, redirect STDOUT to a file or /dev/null. This especially is important for commands executed in the background like the last one you perform just before exiting.

    Without #2, the server will never finish sending the response to your browser, and this might be causing your browser to time-out the connection which might have something to do with your problem.

      Thank you for the reply. I have removed fork and exit and I am using close(STDOUT)
      still not working...facing same problem
        To elaborate, you should append " >/dev/null" to your system command strings (but in the last case insert it before the &).

        Here's some basics on I/O redirection: Standard Input and Output Redirection

        You're still having problems because you didn't do what you were asked to do. pc88mixer asked you to redirect stdout in the system call to something else, like /dev/null, so that the output was not 'attached' to the cgi process. Closing stdout in the perl script isn't the same thing.

Re: system command not working after connection with cgi breaks
by psini (Deacon) on Jun 23, 2008 at 17:30 UTC

    I'm not sure if what you find is a standard behavior and if it's possible avoiding it but, in a similar situation, I resolved the problem executing a at now ... command instead of forking. It's a hack, but it works.

    Careful with that hash Eugene.

      can you please elaborate on at now command

        You make a system call to the at unix command (see man at).

        In our context, if you execute at now <"your_command" the at command returns immediately, so you can terminate your cgi script but the command "your_command" is executed in another process that is not a child of your process, but of cron deamon. Actually at schedules a single execution of your_command via crond.

        Careful with that hash Eugene.