2016-08-11 95 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