Wil*_*ell 125
如果您想等待工作完成,请使用wait.这将使shell等待所有后台作业完成.但是,如果你的任何作业自己守护,它们不再是shell的子代,等待就没有效果了(就shell而言,这个孩子已经完成了.事实上,当一个进程守护自己时,它会这样做通过终止和产生一个继承其角色的新进程来实现.
#!/bin/sh
{ sleep 5; echo waking up after 5 seconds; } &
{ sleep 1; echo waking up after 1 second; } &
wait
echo all jobs are done!
Suk*_*uku 10
您可以使用它kill -0来检查特定pid是否正在运行.
假设您在pwd中pid调用的文件中有数字列表pid
while true;
do 
    if [ -s pid ] ; then
        for pid in `cat pid`
        do  
            echo "Checking the $pid"
            kill -0 "$pid" 2>/dev/null || sed -i "/^$pid$/d" pid
        done
    else
        echo "All your process completed" ## Do what you want here... here all your pids are in finished stated
        break
    fi
done