# simulate a long process that will have an identifiable exit code
(sleep 15 ; /bin/false) &
my_pid=$!
while ps | grep " $my_pid " # might also need | grep -v grep here
do
echo $my_pid is still in the ps output. Must still be running.
sleep 3
done
echo Oh, it looks like the process is done.
wait $my_pid
# The variable $? always holds the exit code of the last command to finish.
# Here it holds the exit code of $my_pid, since wait exits with that code.
my_status=$?
echo The exit status of the process was $my_status
你直接这么wait是不行的,你得wait这几个后台进程的pid才行
这样如果任意一个失败则退出码都不是0,最后一个错误退出的退出码应该就是wait的退出码。更完整的
wait
使用你应该看wait --help
或者man wait
,有更多范例Stackoverflow有更完整的范例和解释: https://stackoverflow.com/que...
看看人家的范例,写的非常漂亮: