我跑在我的剧本了多个后台进程退出时,子进程的一个退出
run_gui()
{
exec ... # the real commands here
}
功能run_ai1(), run_ai2
类似于一个bash脚本。
然后我运行的功能,并做必要的管道
run_gui &
run_ai1 &
run_ai2 &
while true; do
while true; do
read -u $ai1_outfd line || echo "Nothing read"
if [[ $line ]]; then
: # processing
fi
done
sleep $turndelay
while true; do
read -u $ai2_outfd line || echo "nothing read"
if [[ $line ]]; then
: # processing
fi
done
sleep $turndelay
done
如果任何这三个进程退出的,我要检查他们的退出代码和终止过程的其余部分。例如,如果run_ai2
以退出代码3退出,那么我想要停止进程run_ai1
和run_gui
并退出带有退出代码1的主脚本。不同背景进程的正确退出代码可能有所不同。
问题是:我如何检测它?有命令wait
,但我事先不知道哪个脚本会先完成。我可以运行wait
作为后台进程 - 但它变得更加笨拙。
你能帮助我吗?
陷阱SIGCHLD,它允许在执行一些孩子最终声明 –
您可以用[期待](HTTP://期待.sourceforge.net /)?在Expect中,捕获退出子进程比POSIX shell或Bash更优雅。 – nwk
@nwk我期望写一个bash脚本。所以我怀疑这将被允许。 – marmistrz