Blog

03/06/2019

What does the sleep command do in Linux?



Can you explain to me the sleep command in Linux? I read somewhere that it can pause a bash shell script. How can I use the Linux sleep command to pause a bash script?

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?

  1. /bin/sleep is Linux or Unix command to delay for a specified amount of time.
  2. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues.
  3. In other words, the sleep command pauses the execution on the next shell command for a given time.
  4. GNU version of sleep command supports additional options
  5. For example, suspend a bash shell script or command prompt for five seconds, type: sleep 5
  6. 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:

  1. s for seconds (the default).
  2. m for minutes.
  3. h for hours.
  4. 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: SXI ADMIN {https://sxi.io}
# -----------------------------------------------------------
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.sxi.io

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.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...