Bash wait command:
-----------------------------------
wait command stop script execution until all jobs running in background have terminated, or until the job number or process id specified as an option terminates.
It returns the exit status of waited-for command.
wait can take the job-id or the process number. i.e.
wait%1 or wait $PID
_________
wait ${!}
_________
wait ${!} means "to wait till the last background process is completed" ($! being the PID of the last background process)
________________________
An example on wait command.
________________________
Suppose
- you have a script called sort_db.sh which sorts some data files and takes a lot of time to complete(you definitely want it to run as background process in your script)
- One more script called bkptmp.sh, which does some job of backing up some tmp files(nowhere related to the above sort_db.sh)
- You have to perform some tasks in your script after sort_db.sh and bkptmp.sh complete their individual tasks (Note: both the .sh should be completed before you perform the said operation)
$ cat waittest.sh
#!/bin/sh
./sort_db.sh &
echo "1st Line"
./bkptmp.sh &
echo "2dn line"
wait
echo "Some operation will follow this"
...
...
In such situations the "bash wait command" is useful. It will wait till sort_db.sh and bkptmp.sh get complete their execution.
You might be thinking, we could have run sort_db.sh and bkptmp.sh in foreground, so that the execution of the operation will follow them. The problem is that you don't want to wait for sort_db.sh to complete for bkptmp.sh to start (I told earlier, they are not dependent). So using wait, the time sort_db.sh gets completed, we will be done(or almost done) with bkptmp.sh.
No comments:
Post a Comment