1833283 Members
3293 Online
110051 Solutions
New Discussion

Re: "wait" in a script

 
SOLVED
Go to solution
ROSS HANSON
Regular Advisor

"wait" in a script

Hello,

I am trying to write a simple little script
I need to wait for 5-10 seconds before the next
command goes. what is the proper syntax to
put a wait command in a script???

Thank you
hanson
Ross Hanson
3 REPLIES 3
Helen French
Honored Contributor
Solution

Re: "wait" in a script

Hi Ross:

I think you need to use 'sleep' command:

sleep 10 ( wait for 10 seconds)

HTH,
Shiju
Life is a promise, fulfill it!
James R. Ferguson
Acclaimed Contributor

Re: "wait" in a script

Hi:

#!/usr/bin/sh
echo "waiting for 10 seconds..."
sleep 10
echo "all done!"
exit 0

...or to wait for a background task (job) to complete:

#!/usr/bin/sh
sleep 10&
PID=$!
echo "...waiting for background task..."
wait $PID
echo "all done!"
exit 0

Regards!

...JRF...
Darrell Allen
Honored Contributor

Re: "wait" in a script

Hi Ross,

"sleep 10" is how to "pause" execution of a script for 10 seconds.

If you want to run several commands concurrently and wait for them to finish before doing something else, use "wait". For example:

command1 &
command2 &
command3 &
wait
command4

This script runs commands 1 - 3 in the background then waits until they all complete before running command4.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)