如何在bash等几个子过程完成并返回退出code!= 0时,任何与子code结束!= 0? [英] How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?

查看:186
本文介绍了如何在bash等几个子过程完成并返回退出code!= 0时,任何与子code结束!= 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从该脚本产生了几个子过程bash脚本等待完成并返回退出code!= 0时,任何一个子进程与code结束!= 0?

How to wait in a bash script for several subprocesses spawned from that script to finish and return exit code !=0 when any of the subprocesses ends with code !=0 ?

简单的脚本:

#!/bin/bash
for i in `seq 0 9`; do
  doCalculations $i &
done
wait

以上脚本将等待所有10催生子进程,但它总会给退出状态0(见帮助等待)。我怎么能修改此脚本,所以它会发现催生子进程的退出状态,并返回退出code 1时任子进程与code结束!= 0?

The above script will wait for all 10 spawned subprocesses, but it will always give exit status 0 (see help wait). How can I modify this script so it will discover exit statuses of spawned subprocesses and return exit code 1 when any of subprocesses ends with code !=0?

有没有那个比收集子进程的PID任何更好的解决方案,以便与和退出状态等待他们?

Is there any better solution for that than collecting PIDs of the subprocesses, wait for them in order and sum exit statuses?

推荐答案

也(可选)主罚PID的过程中等待,并与$!你在后台启动的最后一个命令的PID。
修改循环存储的PID的每个产生子过程分成数组,然后循环再次等待每个PID

wait also (optionally) takes the PID of the process to wait for, and with $! you get the PID of the last command launched in background. Modify the loop to store the PID of each spawned sub-process into an array, and then loop again waiting on each PID.

这篇关于如何在bash等几个子过程完成并返回退出code!= 0时,任何与子code结束!= 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆