假设我在shell脚本中并行运行两个命令,例如
/cmd1 &
pid1=$!
/cmd2 &
pid2=$!
status1= # what should go here?
status2=$? # I know this will have status of previous background process only
我希望只有两个命令都具有干净的退出状态才能继续 如何查看第一个后台进程的状态。
答案 0 :(得分:0)
一种解决方案是使用GNU Parallel with --joblog,此处显示2个命令exit 0
和exit 1
:
(echo exit 0; echo exit 1) | parallel --joblog /tmp/foo
cat /tmp/foo
Seq Host Starttime Runtime Send Receive Exitval Signal Command
1 : 1359451838.09 0.002 0 0 0 0 true
2 : 1359451838.093 0.002 0 0 1 0 false
Exitval列将给出退出值。如果其中一个作业失败,GNU Parallel也将返回非零值:
(echo exit 0; echo exit 1) | parallel && echo Do this if none fail
或同等的:
parallel ::: "exit 0" "exit 1" && echo Do this if none fail