Obtaining exit status values from GNU parallel

2019-06-26 03:17发布

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?

4条回答
家丑人穷心不美
2楼-- · 2019-06-26 03:59

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
查看更多
神经病院院长
3楼-- · 2019-06-26 04:01

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"
查看更多
孤傲高冷的网名
4楼-- · 2019-06-26 04:01

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);
}
查看更多
仙女界的扛把子
5楼-- · 2019-06-26 04:02

To get the exist status of the individual jobs, parallel would need to write the info somewhere. I don't know if it does or not. If it doesn't, you can do that yourself.

my %jobs = (
   "date"   => "date",
   "ls"     => "ls",
   "pwd"    => "pwd",
   "factor" => "factor 17",
);

open(my $parallel, "|parallel"); 
for my $id (keys(%jobs)) {
   print $parallel
      $jobs{$id}
      ." 1> '/tmp/$id.out'"
      ." 2> '/tmp/$id.err' ; "
      ."echo \$?"
      ." > '/tmp/$id.exit'\n"; 
} 

close($parallel); 

my $exit_status = $? >> 8;
if ($exit_status >= 255) {
    print("Failed\n");
} else {
    printf("%d failed jobs\n", $exit_status);
}

for my $id (keys(%jobs)) {
    ...grab output and exit code from files...
}

Update: I went and installed parallel.

It has an option called --joblog {file} which produces a report with exit codes. It accepts - for file name if you want it to output to STDOUT.

Note that parallel doesn't recognise abnormal death by signal, so this is not included in the --joblog report. Using the solution I posted above, a missing .exit file would indicate an abnormal death. (You must make sure it doesn't exist in the first place, though.)

查看更多
登录 后发表回答