xargs parallel - 捕获退出代码

时间:2016-08-11 18:38:33

标签: parallel-processing

我有一个解析flatfile的shell脚本,并且对于其中的每一行,并行执行一个hive脚本。

  a
  b

问题是我如何捕获每个并行进程的退出代码,因此即使一个子进程失败,最后也会使用错误代码退出脚本。

不幸的是我无法使用gnu parallel。

2 个答案:

答案 0 :(得分:1)

我认为你寻找更好的东西,但一个简单的解决方案是将可能的错误存储在tmp文件中并在之后查找:

FilewithErrors=/tmp/errors.txt
FinalError=0

xargs -P 5 -d $'\n' -n 1 bash -c '
IFS='\t' read -r arg1 arg2 arg 3<<<"$1"
eval "hive -hiveconf tableName=$arg1 -f ../hive/LoadTables.hql || echo $args1 > $FilewithErrors" 2> ../path/LogFile-$arg1
' _ < ../path/TableNames.txt

if [ -e $FilewithErrors ]; then FinalError=1; fi

rm $FilewithErrors

return $FinalError

答案 1 :(得分:-1)

根据评论:使用GNU Parallel作为个人或最小安装,如http://git.savannah.gnu.org/cgit/parallel.git/tree/README

中所述

来自man parallel

退出状态

   Exit status depends on --halt-on-error if one of these are used: success=X,
   success=Y%, fail=Y%.

   0     All jobs ran without error. If success=X is used: X jobs ran without
         error. If success=Y% is used: Y% of the jobs ran without error.

   1-100 Some of the jobs failed. The exit status gives the number of failed jobs.
         If Y% is used the exit status is the percentage of jobs that failed.

   101   More than 100 jobs failed.

   255   Other error.

如果您需要确切的错误代码(而不仅仅是作业是否失败),请使用:--joblog mylog

您可以执行以下操作:

cat ../path/TableNames.txt |
  parallel --colsep '\t' --halt now,fail=1 hive -hiveconf tableName={1} -f ../hive/LoadTables.hql '2>' ../path/LogFile-{1}
如果一个作业失败,

fail=1将停止生成新作业,并退出作业中的退出代码。

now会杀死剩余的工作。如果您希望其余作业退出“自然原因”,请改用soon