You can use sleep command to pause execution of shell scripts or commands for a given period on a Linux or Unix-like systems. This page explains syntax and usage of the sleep command in Linux operating systems.
So, what does the sleep command do in Linux?
- /bin/sleep is Linux or Unix command to delay for a specified amount of time.
- You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues.
- In other words, the sleep command pauses the execution on the next shell command for a given time.
- GNU version of sleep command supports additional options
- For example, suspend a bash shell script or command prompt for five seconds, type: sleep 5
- Common examples of sleep commands include scheduling tasks and delaying the execution to allow a process to start. Another usage is waiting until a wifi network connection available to stream large file over the network.
Sleep command syntax
The syntax for the sleep command is as follows:
sleep NUMBER[SUFFIX]
In addition to seconds, [SUFFIX] can be as follows on GNU/Linux:
- s for seconds (the default).
- m for minutes.
- h for hours.
- d for days.
Above options are only supported on GNU version of Linux and not on macOS/Unix/*BSD family of oses. Therefore, for non-GNU/Linux system try:
sleep 5
sleep 2
Sleep command examples
To sleep for 13 seconds, use:
sleep 13
For instance, sleep for 0.5 or 2.5 seconds too, try:
sleep 0.5
OR
sleep 2.5
So a floating point number allowed. However, sleep 2h30m not allowed. Want to sleep for 2 minutes? Try:
sleep 2m
Halt or sleep for 2 hours, use:
sleep 2h
First sleep for 8 hours and after that play music file named wake-up.mp3
sleep 8h && mplayer wake-up.mp3
How to use the Linux sleep command to pause a bash script
Let us see a simple example that pause script for 10 seconds.
#!/bin/bash # Name: sleep-demo.sh # Purpose: bash script examples that demos sleep command # Author: Vivek Gite {https://www.cyberciti.biz} # ----------------------------------------------------------- SLEEP_TIME="10" echo "Current time: $(date +%T)" echo "Hi, I'm sleeping for ${SLEEP_TIME} seconds ..." sleep ${SLEEP_TIME} echo "All done and current time: $(date +%T)"
Run it as follows (see how to run shell script in Linux for more information):
chmod +x sleep-demo.sh
./sleep-demo.sh
Sleep command in action
sleep command shell script examples
The shell script will start by showing current time on screen. After that, our shell script tells you how to quit and will continue to display current time on screen:
#!/bin/bash ## run while loop to display date and hostname on screen ## while [ : ] do clear tput cup 5 5 echo "$(date) [ press CTRL+C to stop ]" tput cup 6 5 sleep 1 done
How to pause my bash shell script for 10 second before continuing
Try the read command as follows:
read -p "text" -t seconds read -p "Waiting 10 seconds for CDN/Cloud to clear cache from proxy server" -t 10 echo "Uploading a fresh version of gif file now ...." ./upload.py "$gif_file" server1.cyberciti.biz
Let’s take a look at a more advanced example of sleep
mkfifo $FIFO || exit 1 flush_and_wait | "$MYSQL" $MYSQL_OPTS & # wait until every block is flushed # # add sleep 1 second to slow down a bit # while [ "$(echo 'SHOW STATUS LIKE "Key_blocks_not_flushed"' |\ "$MYSQL" $MYSQL_OPTS | tail -1 | cut -f 2)" -gt 0 ]; do sleep 1 done
Another example that shows advanced usage of sleep command:
# workaround for a possible latency caused by udev, sleep max. 10s if kernel_is_2_6_or_above ; then for x in `seq 100` ; do (exec 6<> /dev/net/tun) > /dev/null 2>&1 && break; sleep 0.1 done fi
Conclusion
You learned how to use the Linux sleep command. It is one of the most straightforward commands, and it only accepts one command line argument which states the sleep interval. It is similarly to the clear command that only accepts one argument on Linux. For more information see GNU docs here.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
If you have any question or feedback, feel free to leave a comment on our forum thread here.