views:

756

answers:

2

Is there any built in feature in bash to wait for any process to finish? We use " wait" only for the child processes to finish. I would like to know if there is any way to wait for any process to finish before proceeding in any script.

A mechanical way to do this is as follows but I would like to know if there is any built in feature in bash.

while ps -p cat $PID_FILE > /dev/null; do sleep 1; done

A: 

There is no builtin feature to wait for any process to finish.

You could send kill -0's to any PID found, so you don't get puzzled by zombies and stuff that will still be visible in ps (while still retrieving the PID list using ps).

TheBonsai
+3  A: 

There's no builtin. Use kill -0 in a loop for a workable solution:

anywait(){

    for pid in "$@"; do
        while kill -0 "$pid"; do
            sleep 0.5
        done
    done
}
Teddy
Never found better. you can add a timeout parameter that kill -15 then kill -9 if the process get stuck ...
neuro