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

Dear monks, novices, postulants, aspirants, benefactors,...

I have a Perl script which submits several jobs to an SGE grid cluster, later jobs being dependent on the output of previous ones (I use -sync yes to make sure they are run in order). Until now, I simply submitted the jobs using system, but I would like to achieve that the main script dies when a job in the sequence fails, instead of pointlessly going on. Since this occurs several times in my code, I used a subroutine to code it:

&qsub("qsub $qsub_options $subscript $script_args", "Some files are broken");
In the subroutine, I want to capture the exit code and possible message printed by SGE so I can potentially terminate the main script. I looked up the module IO::CaptureOutput and tried the following:
sub qsub { my ($cmd, $error_message) = @_; my @cmd = @{ $cmd }; my ($stdout,$stderr,$success,$exit_code) = capture_exec(@cmd); if (($exit_code > 0) || ($stderr)) { die "$error_message\n"; } }

This doesn't work, however. I get an error related to strict because of the @cmd array, but I don't understand why it has to be an array. Actually, I tried using a scalar first, but it didn't work either.

For reference, the relevant part of the SGE man page says: "If -sync y is used ... If all the job's tasks complete successfully, qsub's exit code will be ... 0 ... If any of the job's tasks fail ..., qsub will print out an error message ... and will have an exit code of 1".

Can somebody explain what's going on and help me fix it?

Replies are listed 'Best First'.
Re: Capturing SGE exit code in Perl
by Anonymous Monk on May 28, 2012 at 20:46 UTC
Re: Capturing SGE exit code in Perl
by runrig (Abbot) on May 31, 2012 at 16:43 UTC
    I get an error related to strict because of the @cmd array

    So, is the $cmd you pass in a reference to an array or not? If so, you can just supply @$cmd as the argument to capture_exec. If not, then what is it? Just the command with no arguments? Then you should be able to just pass $cmd to capture_exec (if there are no arguments).

    When learning/testing command execution code, its easiest to start with something simple, like ls -l. Then ls -l file1 file2 (assuming file1 and file2 exist), and to test what happens on error, something like ls -l file1 file_that_does_not_exist.

Re: Capturing SGE exit code in Perl
by thargas (Deacon) on May 30, 2012 at 18:39 UTC

      Sorry :-/ Most of the time, people who don't know about "XYZ" won't know the answer anyway, so they need not read, but this was a question people who don't know the Sun Grid Engine could have helped with. I should have just left it out.