Bash Sleep
The sleep command is very easy. You basically just need to specify the number of seconds that you want to sleep for.
To sleep for 5 seconds:
sleep 5
To sleep for 10 minutes ( 600 seconds ):
sleep 600
Why use the sleep command?
The sleep command is useful to add a delay to scripts. This is useful if you don’t want to run the same command too frequently. It is also useful to wait for something that might be delayed. It works great in loops. It allows you to check back every 5 seconds rather than flooding the system with hundreds of requests per second.
Sleep Command in Bash Script Examples
These examples assume the existence of the following made up example tools:
./request_data |
./process_data |
./check_ready |
You could use a simple script like this to wait until data is available before running the next command. This works if you happen to know about how long it would take for the data to be available.
You need to be able to get the timing right for an example like this to work.
timing_example.sh./request_data sleep 30 ./process_data
This example uses loop to check back every 5 seconds until data is ready to be processed. It loops infinitly until ready.
./request_data
while true; do
if ./check_ready; then
break
fi
sleep 5
done
./process_data
Suffix
You can optionally specify a suffix ( Linux only, not BSD or MacOS ).
Available suffix:
s | seconds ( default ) |
m | minutes |
h | hours |
d | days |
Sleep for 30 seconds:
sleep 30s
Sleep for 5 minutes:
sleep 5m
Sleep for 2 hours:
sleep 2h