'Obtaining exit status values from GNU parallel

The Perl wrapper below executes commands in parallel, saving STDOUT and STDERR to /tmp files:

open(A,"|parallel"); 
for $i ("date", "ls", "pwd", "factor 17") { 
  print A "$i 1> '/tmp/$i.out' 2> '/tmp/$i.err'\n"; 
} 
close(A); 

How do I obtain the exit status values from the individual commands?



Solution 1:[1]

GNU Parallel 20110722 has exit val and signal in --joblog:

parallel --joblog /tmp/log false ::: a
cat /tmp/log
Seq     Host    Starttime       Runtime Send    Receive Exitval Signal  Command
1       :       1311332758      0       0       0       1       0       false a

Solution 2:[2]

If you want to avoid the wrapper you could consider:

cat foo | parallel "{} >\$PARALLEL_SEQ.out 2>\$PARALLEL_SEQ.err; echo \$? >\$PARALLEL_SEQ.status"

Version 20110422 or later makes it even shorter:

cat foo | parallel "{} >{#}.out 2>{#}.err; echo \$? >{#}.status"

If your lines do no contain ' then this should work too:

cat foo | parallel "{} >'{}'.out 2>'{}'.err; echo \$? >'{}'.status"

Solution 3:[3]

Instead of wrapping parallel, you can use any of the tons of modules available from CPAN providing similar functionality.

For instance:

use Proc::Queue size => 10, qw(run_back);

my @pids;

for $i ("date", "ls", "pwd", "factor 17") {
  push @pids, run_back {
    open STDOUT, '>', '/tmp/$i.out';
    open STDERR, '>', '/tmp/$i.err';
    exec $i;
  }
}

for (@pids) {
  1 while waitfor($_, 0) <= 0;
  say "process $_ exit code: ", ($? >> 8);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ole Tange
Solution 2 Ole Tange
Solution 3 salva