gnu parallel re-运行 当它因 while 循环而失败时

gnu parellel re-run when it fails with a while loop

假设我们有一个 csv 文件

1
2
3
4

代码如下:

cat A.csv | while read A; do

echo "echo $A" > $A.sh

echo "$A.sh"

done | xargs -I {} parallel --joblog test.log --jobs 2 -k sh ::: {}

以上是一个简化的案例。但几乎得到了大部分。这里的并行将运行像这样:

parallel --joblog test.log --jobs 2 -k sh ::: 1.sh 2.sh 3.sh 4.sh

现在假设 3.sh 由于某些原因失败了。是否有任何简单的方法可以在同一行并行命令中重新 运行 当前 shell 脚本设置中失败的 3.sh?我尝试了以下方法,但它不起作用而且很长。

cat A.csv | while read A; do

echo "echo $A" > $A.sh

echo "$A.sh"

done | xargs -I {} parallel --joblog test.log --jobs 2 -k sh ::: {}

# The above will do this:

# parallel --joblog test.log --jobs 2 -k sh ::: 1.sh 2.sh 3.sh 4.sh

cat A.csv | while read A; do

echo "echo $A" > $A.sh

echo "$A.sh"

done | xargs -I {} parallel --resume-failed --joblog test.log --jobs 2 -k sh ::: {}  

# The above will do this:

# parallel --resume-failed --joblog test.log --jobs 2 -k sh ::: 1.sh 2.sh 3.sh 4.sh
######## 2017-09-25

谢谢奥莱。我尝试了以下

doit() {
   myarg=""

if [ $myarg -eq 3 ]

then
        exit 1

else

echo do crazy stuff with "$myarg"

fi

}
export -f doit



parallel -k --retries 3 --joblog ole.log doit :::: A.csv

它returns日志文件是这样的:

Seq     Host    Starttime       JobRuntime      Send    Receive Exitval Signal  Command
1       :       1506362303.003       0.016      0       22      0       0       doit 1
2       :       1506362303.006       0.013      0       22      0       0       doit 2
3       :       1506362303.026       0.002      0       0       1       0       doit 3
4       :       1506362303.014       0.006      0       22      0       0       doit 4

但是,我没有看到 doit 3 按预期重试 3 次。能开导一下吗?谢谢。

首先:生成 .sh 文件似乎不是个好主意。您很可能只创建一个函数:

doit() {
   myarg=""
   echo do crazy stuff with "$myarg"
}
export -f doit

要重试失败的命令,请使用 --retries:

parallel --retries 3 doit :::: file.csv

如果您的 CSV 文件有多个列,请使用 --colsep:

parallel --retries 3 --colsep '\t' doit :::: file.csv

使用这个:

doit() {
   myarg=""

  if [ $myarg -eq 3 ] ; then
      echo do not do crazy stuff with "$myarg"
      exit 1
  else
      echo do crazy stuff with "$myarg"
  fi
}
export -f doit

这将重试“3”作业 3 次:

parallel -k --retries 3 --joblog ole.log doit ::: 1 2 3 4

只会记录最后一次。要确信这实际上是 运行 三次,运行 输出无缓冲:

parallel -u --retries 3 --joblog ole.log doit ::: 1 2 3 4