Saturday, June 7, 2008

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.

10 comments:

tijuanabsd said...

thanks, dude!! this is exactly what i was looking for!!

Nat said...

Same here, thanks!

Nicholas Brookins said...

This was useful, couldn't remember quite how wait worked. Then I noticed a familiar face on the blog.. Hi Jadu! :)

--Nick

Jadu Saikia said...

@Nick, Hey, thanks a lot Nick.

mvvak said...

Thanks. Useful and to the point, Jadu.

Bala Guru said...

in my case wait is not working.

i have a scenario like this. i have three script files

1. a.sh
2. b.sh
c. main.sh

i will pass a.sh and b.sh as parameter to main.sh like

sh main.sh a.sh & b.sh

content of main.sh
#!/bin/sh
$1
wait
exit 0

wait is not working. it passes to other command. please help me in resolving this.

Bala Guru said...

Wait is not working in my scenario.

i have 3 shell script. a.sh,b.sh,main.sh

main.sh content
#!/bin/ksh
$1
wait
exit 0

i execute main.sh as follows
sh main.sh a.sh & b.sh

please help me in resolving this.

Jadu Saikia said...

@Bala, this should work:

e.g.


#!/bin/sh
echo "Inside $(basename $0)"
sleep 10


#!/bin/sh
./$1 &
wait
exit 0

Now, execute:
$ ./main.sh a.sh b.sh

Hope this helps.

Joshua Tsang said...

This is brilliant - thanks! Succinct and clear!

Jadu Saikia said...

wait' is very useful in a case like this:

Assume you ran multiple background commands and then you have a requirement to start a service when the background jobs are done, you may not like to wait till they are complete. In such a case:


$ command1 &
$ command2 &
$ command3 &
$ wait && start_service


Useful ?

© Jadu Saikia www.UNIXCL.com