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

Hi All,
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 System command executes till the connection with cgi is alive. After that it doesnot execute. Can you please suggest how can I redirect the system command output. So that my perl script can still execute the system command even if the cgi dies. I donot have any admin rights on server
thank you
  • Comment on How to redirect the output of system call

Replies are listed 'Best First'.
Re: How to redirect the output of system call
by kyle (Abbot) on Jul 08, 2008 at 17:47 UTC
      I tried all of them still its not working.
      $command ="nice R --slave --args $DirName $SubDirName < read_param +eters_by_file_batch.R > $SubDir?outR.txt"; my $oput=`$command`;
      I am lost
        my $oput=`$command` waits for $command to finish. It seems that you don't want this, so you need to start it in the background. Probably not with `...`.

        There are quite some suggestions in the previous threads as well as in the article by merlyn I linked in this thread. Go read about them, and try to fix it.

        It looks like you are forgetting to include the ampersand at the end, which is the thing that tells the shell to run the command as a background job. Also, it looks like you have a shell "magic" character (question mark) in the output file name -- I think that was supposed to be a slash? And I wonder about your variable names... Are "$SubDirName" and "$SubDir" supposed to be two different strings? Finally, as mentioned above, you really don't want to run the command in backticks.

        That looks to me like four different errors in just two lines of code. I guess you are lost.

        Try it like this (some of the changes relative to your two lines are just meant to make it look better):

        my $ifile = "read_parameters_by_file_batch"; my $ofile = "outR.txt"; my @args = ( $DirName, $SubDirName ); my $cmd = "R --slave --args @args < $ifile > $SubDirName/$ofile 2> +/dev/null &" my $failed = system( $cmd ); if ( $failed ) { die "$0: command failed: ($failed) $!\n trying: $cmd\n"; }
        Try running it that way from a shell first, to see if it does what you want (there's a chance you might need to fix something in the command line syntax, so be sure you have that right before trying it via http).

        Then, when you try it in your cgi script (or whatever), if it fails, be sure to check the server's error log. If you can't do that, try using the "FatalsToBrowser" trick in CGI, so that the error report goes to the browser.

        If you still have trouble and decide you want to post another question, you had better include the actual code you are using, and the actual error message(s) you are getting. Just saying "it doesn't work" is no way to make progress. If necessary, create a small script that does nothing else except demonstrate the problem, so you can post it in full. That will give us more of what we need in order to help.

Re: How to redirect the output of system call
by moritz (Cardinal) on Jul 08, 2008 at 17:45 UTC
    To redirect output from a call to system, use shell level redirection:
    system('your_command --with options > /other/file');

    To keep a CGI script running after the connection quits, use fork and close or re-open STDOUT and STDERR. See also Watching long processes through CGI.