1:在bash,$!保存已执行的最后一个后台进程的PID.无论如何,这将告诉您要监控的流程.
4:等待< n>等待直到具有PID< n>的过程.已完成(它将阻塞直到进程完成,因此您可能不希望在确定进程完成之前调用此方法),然后返回已完成进程的退出代码.
2,3:ps或ps | grep“$!”可以告诉您进程是否仍在运行.由您决定如何理解输出并决定它与完成的接近程度. (ps | grep不是白痴.如果你有时间,你可以想出一个更健壮的方法来判断这个过程是否还在运行).
这是一个骨架脚本:
# 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
668

被折叠的 条评论
为什么被折叠?



