Source From Here
Question
Is there any built-in feature in bash to wait for any process to finish? The wait command only allows one to wait for 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:
- waitProc.sh
Usage example:
How-To
There's no builtin. Use kill -0 in a loop for a workable solution:
- waitProc2.sh
As noted by several commentators, if you want to wait for processes that you do not have the privilege to send signals to, you have found some other way to detect if the process is running to replace the
kill -0 $pidcall. On Linux, test -d "/proc/$pid" works, on other systems you might have to use pgrep (if available) or something like ps | grep ^$pid.
Usage example:
Question
Is there any built-in feature in bash to wait for any process to finish? The wait command only allows one to wait for 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:
- waitProc.sh
- #!/bin/sh
- while ps -p $1 > /dev/null; do
- echo -e "Wait PID=$1..."
- sleep 1;
- done
- echo -e "Exit Shell"
How-To
There's no builtin. Use kill -0 in a loop for a workable solution:
- waitProc2.sh
- #!/bin/sh
- waitPID(){
- while kill -0 "$1" >> /dev/null 2>&1; do
- sleep 0.5
- done
- }
- echo -e "Wait PID=$1..."
- $(waitPID $1)
- echo -e "Exit Shell..."
Usage example:
沒有留言:
張貼留言