Linux Sleep Command (Pause a Bash Script)

Introduction

sleep is a command-line tool that suspends the caller process for a set amount of time. To put it another way, the sleep command stops the execution of the next command for a specified amount of time.

When used within a bash shell script, such as when retrying a failed action or inside a loop, the sleep command comes in handy.

In this tutorial we'll show you how to utilize the Linux sleep command. We will also address a few FAQs on Linux Sleep Command (Pause a Bash Script).

How to Use the sleep Command

Below is the syntax for sleep command:

sleep NUMBER[SUFFIX]...

A positive integer or a floating-point number can be used as the NUMBER.

One of the following could be the SUFFIX:

  • s - seconds (default)
  • m - minutes
  • h - hours
  • d - days

When no suffix is supplied, seconds is used by default.

When there are two or more arguments, the total time is equal to the sum of their values.

Here are a few straightforward examples of how to utilize the sleep command:

  • Sleep for 5 seconds:
sleep 5
  • Sleep for 0.5 seconds:
sleep 0.5
  • Sleep for 2 minute and 30 seconds:
sleep 2m 30s

Bash Script Examples

We'll go over a few basic shell scripts in this part to show how the sleep command is utilized.

#!/bin/bash

# start time
date +"%H:%M:%S"

# sleep for 5 seconds
sleep 5

# end time
date +"%H:%M:%S"

The script will print the current time in HH:MM:SS format when you run it. The sleep command then takes a 5-second break from the script. The script's last line prints the current time once the specified time period has passed.

This is what the final product will look like:

Output

13:34:40
13:34:45

Let's look at an example that is a little more advanced:

#!/bin/bash

while :
do
  if ping -c 1 ip_address &> /dev/null
  then
    echo "Host is online"
    break
  fi
  sleep 5
done

Every 5 seconds, the script checks whether a host is online or not. The script will alert you and halt when the host goes online.

The way the script works is as follows:

  • We're going to make an infinite while loop in the first line.
  • The ping command is then used to see if the host with the IP address ip_address is reachable or not.
  • The script will echo "Host is online" and end the loop if the host is reachable.
  • If the host cannot be reached, the sleep command stops the script for 5 seconds before restarting the loop.

FAQs on Linux Sleep Command (Pause a Bash Script)

How can I use the sleep command to pause a script for a specific duration? 

You can use the command "sleep <duration>" where <duration> can be specified in seconds, minutes, hours, or a combination. For example, "sleep 5" will pause for 5 seconds.

Can I specify time in minutes instead of seconds? 

Yes, you can use the suffix 'm' to denote minutes. For instance, "sleep 1m" will pause for 1 minute.

How can I pause a script for a specific number of hours? 

Use the 'h' suffix for specifying hours. For example, "sleep 2h" will pause the script for 2 hours.

What if I want to pause a script for a fraction of a second?

Use the decimal format. For instance, "sleep 0.5" will pause for half a second.

Can I pause a script for dynamically calculated durations? 

Yes, you can use variables or command substitution to calculate the duration before passing it to the sleep command.

How can I interrupt the sleep command and resume the script execution? 

Pressing Ctrl+C will terminate the sleep command and resume the script execution immediately.

Is it possible to display a countdown while the script is paused? 

Yes, you can use a loop to display a countdown by repeatedly printing the remaining time during the sleep.

Conclusion

One of the most basic Linux commands is the sleep command. It's used to pause the next command's execution for a set amount of time.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.