2016-08-11 50 views
1

我有一个shell脚本,用于解析flatfile文件,并为其中的每一行并行执行配置单元脚本。xargs并行 - 捕获退出代码

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" 2> ../path/LogFile-$arg1 
    ' _ < ../path/TableNames.txt 

问题是我怎么可以捕捉退出代码从每个并行处理,因此,即使一个子进程失败,在与错误代码年底退出脚本。

不幸的是我不能使用gnu parallel。

+0

你能详细说明为什么你不能使用GNU并行? https://oletange.blogspot.dk/2013/04/why-not-install-gnu-parallel.html上涵盖的原因是什么? –

+1

感谢您的信息。我认为我们必须安装gnu parallel(我们不允许安装任何软件)才能使用它。从来没有意识到有一个选项可以复制并行文件并使用它。 – snate

+0

最后在我们的环境中安装了gnu并行处理器。目前我的上面的脚本使用xargs,我怎样才能用并行来替换它,执行像解析平面文件一样的操作,并且为它的每一行执行一个hive脚本并行地执行退出状态捕获和失败逻辑。谢谢。 – snate

回答

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 
0

按照评论:安装为一个使用GNU并行如在http://git.savannah.gnu.org/cgit/parallel.git/tree/README

描述从man parallel

EXIT STATUS个人或最小化安装

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