如何在后台循环检查多个进程的退出状态? [英] How can I check the exit status of multiple processes in background in loop?
问题描述
我在后台有一个脚本循环
I have a loop with a script in background
while read host
do
./script &
done
wait #waits till all the background processes are finished
但是我想检查程序的退出状态,我该怎么做
but i want to check the exit status of the proceess the how would i do it
while read host
do
./script &
wait $! || let "FAIL+=1"
done
wait
echo $fail
但是上面的代码将并行执行,因为时间是一个重要因素对我来说,所以我想对所有主机进行并行执行.
But the will the above code execute parallely because time is an important factor for me so i want to have parallel execution for all hosts.
这有可能知道哪个进程失败了,以便我可以做
Is this possible to know which process failed so that i can do
echo "these are the list of process ids in background that failed"
12346
43561
.....
并且可以在后台运行的并行进程的数量没有任何限制.在上面的循环中运行大约20个并行过程是否安全
And is there any limit to the number of parallel proceses that can be run in background. is it safe to run about 20 parallel proceses in the above loop
推荐答案
您可以将其添加到脚本的末尾:
You can add this to the end of your script:
RC=$?
test $RC -eq 0 || echo "$$ failed"
exit $RC
$$返回shell的PID.您的背景脚本将在各自独立的shell中运行.
$$ returns the PID of the shell. Your backgrounded scripts will run in their own separate shells.
exit $ RC
行显然是可选的.
这篇关于如何在后台循环检查多个进程的退出状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!