[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMay 5, 2023

How to use Linux Sleep Command (Pause a Bash Script).

How To Use Linux Sleep Command (Pause A Bash Script).

In this tutorial, we will show you how to use the Linux sleep command.

The sleep command is useful when used within a bash shell script, for example, when retrying a failed operation or inside a loop.

sleep is a command-line utility that allows you to suspends the calling process for a specified time. In other words, the sleep command pauses the execution of the next command for a given number of seconds.

 

How to Use the sleep Command

The syntax for the sleep command is as follows:

sleep NUMBER[SUFFIX]...

Copy

The NUMBER may be a positive integer or a floating-point number.

The SUFFIX may be one of the following:

  • s – seconds (default)

  • m – minutes

  • h – hours

  • d – days

When no suffix is specified, it defaults to seconds.

When two or more arguments are given, the total amount of time is equivalent to the sum of their values.

Here are a few simple examples demonstrating how to use the sleep command:

  • Sleep for 5 seconds:

    sleep 5

    Copy

  • Sleep for 0.5 seconds:

    sleep 0.5

    Copy

  • Sleep for 2 minute and 30 seconds:

    sleep 2m 30s

    Copy

Bash Script Examples

In this section, we’ll go over a few basic shell scripts to see how the sleep command is used.

#!/bin/bash

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

# sleep for 5 seconds
sleep 5

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

Copy

When you run the script, it will print the current time in HH:MM:SS format. Then the sleep command pauses the script for 5 seconds. Once the specified time period elapses, the last line of the script prints the current time.

The output will look something like this:

13:34:40
13:34:45
Copy

Let’s take a look at a more advanced example:

#!/bin/bash

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

Copy

The script checks whether a host is online or not every 5 seconds. When the host goes online, the script will notify you and stop.

How the script works:

  • In the first line, we are creating an infinite while loop .

  • Then we are using the ping command to determine whether the host with IP address of ip_address is reachable or not.

  • If the host is reachable, the script will echo “Host is online” and terminate the loop.

  • If the host is not reachable, the sleep command pauses the script for 5 seconds, and then the loop starts from the beginning.

 

Conclusion

The sleep command is one of the simplest Linux commands. It is used to pause the execution of the next command for a given amount of time.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM