How to Use the Linux sleep Command with Examples

Introduction

The Linux sleep command is a useful tool for scheduling tasks and controlling the timing of scripts. It allows you to pause a script for a specified amount of time, allowing you to control the timing of your scripts. In this tutorial, we will discuss the basics of the Linux sleep command and provide some examples of how to use it. We will also discuss some of the more advanced features of the command and how they can be used to create more complex scripts. By the end of this tutorial, you should have a good understanding of how to use the Linux sleep command.

How to Use the Linux sleep Command with Examples

The Linux sleep command is a simple command that pauses a script or program for a specified amount of time. It is useful for delaying the execution of a script or program for a certain amount of time, or for waiting for a certain event to occur.

Syntax

The syntax for the Linux sleep command is as follows:

sleep [time]

Where [time] is the amount of time to pause the script or program, in seconds.

Examples

1. To pause a script or program for 10 seconds, use the following command:

sleep 10

2. To pause a script or program for 5 minutes, use the following command:

sleep 300

3. To pause a script or program until a certain time, use the following command:

sleep $(date -d “10:00” +%s)

This command will pause the script or program until 10:00am.

Introduction

When the user issues a multiple command sequence in Linux, the commands execute immediately one after another or concurrently (e.g., the tee command). However, sometimes it is necessary to postpone the execution of commands and provide enough time for the system to produce the expected results.

In this tutorial, you will learn how to use the Linux sleep command to delay command execution in the terminal and shell scripts.

How to Use the Linux sleep Command with Examples

Prerequisites

  • A system running Linux
  • Access to the command line

What Does the Linux sleep Command Do?

The sleep command suspends the calling process of the next command for a specified amount of time. This property is useful when the following command’s execution depends on the successful completion of a previous command.

Linux sleep Command Syntax Explained

The syntax of the sleep command is simple:

sleep [number]
Using the sleep command to introduce a 5-second delay in the terminal

In the example above, after sleep 5 was executed, the second command prompt appeared with a 5-second delay.

By default, the system reads the number after sleep as the number of seconds. To specify other time units, use the following syntax:

sleep [number][unit]
Using the sleep command to introduce a longer delay in the terminal

The sleep command accepts floating-point numbers. It allows multiple values, which are all added together to calculate the duration of sleep.

Available units are:

  • s – seconds
  • m – minutes
  • h – hours
  • d – days

To stop sleep after it started and before the specified waiting period ends, press Ctrl + C.

To see help for the sleep command, type:

sleep --help 

For version details, type:

sleep --version

Linux sleep Command Examples

The following sections contain examples of using the sleep command in the terminal or shell scripts.

Note: The sleep command is designed to work in combination with other Linux commands. For a list of available Linux commands, download our free Linux Commands Cheat Sheet.

Set up an Alarm

Use sleep to tell the system to play an mp3 file after a certain amount of time. The example uses mplayer:

sleep 7h 30m && mplayer alarm.mp3

Delay Commands in Terminal

sleep is useful for enforcing a time between the execution of two commands. The following example makes echo commands execute in one-second intervals:

sleep 1 && echo "one" && sleep 1 && echo "two"
Introducing a one-second delay between command execution using the sleep command

Assign a Variable to the sleep Command

It is possible to assign a variable to the sleep command. Consider the following shell script:

#!/bin/bash
SLEEP_INTERVAL="30"
CURRENT_TIME=$(date +"%T")
echo "Time before sleep: ${CURRENT_TIME}"
echo "Sleeping for ${SLEEP_INTERVAL} seconds"
sleep ${SLEEP_INTERVAL}
CURRENT_TIME=$(date +"%T")
echo "Time after sleep: ${CURRENT_TIME}"

The script defines a variable called SLEEP_INTERVAL whose value is later used as an argument to the sleep command. The output of this example script shows that the execution lasted 30 seconds:

Output of a script that shows the current time at the beginning and end of the execution

Define Check Intervals

The following example illustrates the use of the sleep command in a script that checks whether a website is online. The script stops if it successfully pings a website, and sleep introduces a 10-second delay between unsuccessful pings.

#!/bin/bash
while :
    do
        if ping -c 1 www.google.com &> /dev/null
        then
        echo "Google is online"
        break
        fi
    sleep 10
done
A script that checks availability of a website outputs a message that the website is online

Allow Time for Operation Completion

You may be running a bash script that internally calls two other bash scripts – one that runs tests in the background and another that prints the results. Use sleep to prevent the second script from printing the wrong results if it executes before the completion of the first script:

while kill -0 $BACK_PID ; do
    echo "Waiting for the process to end"
    sleep 1
done

The kill -0 $BACK_PID command checks if the first script’s process is still running. If it is, it prints the message and sleeps for 1 second before checking again.

Predict Latency

Use sleep to allow latency of certain command executions. The script snippet below shows how sleep gives the CPU enough time to perform the calculation before the next iteration.

for (( i = 1 ; i <= 250 ; i++ )); 
    do  
    sleep 1
    qsub computation"${i}".pbs
done

Conclusion

After reading this tutorial, you should know how to use the Linux sleep command to pause the execution of the commands in a sequence.

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.

How to Use the Linux sleep Command with Examples

The Linux sleep command is a useful tool for delaying the execution of a command or script for a specified amount of time. It is a built-in command that is available on most Linux distributions. In this article, we will explain how to use the sleep command with examples.

Syntax

The syntax of the sleep command is as follows:

sleep [OPTION] NUMBER[SUFFIX]...

Where:

  • OPTION is an optional argument that can be used to specify the type of delay.
  • NUMBER is the amount of time to delay in seconds.
  • SUFFIX is an optional argument that can be used to specify the unit of time.

Options

The sleep command has the following options:

  • -h : Display help information.
  • -v : Display version information.

Examples

Let’s look at some examples of how to use the sleep command.

Delay for 5 Seconds

To delay for 5 seconds, you can use the following command:

sleep 5

Delay for 1 Minute

To delay for 1 minute, you can use the following command:

sleep 1m

Delay for 1 Hour

To delay for 1 hour, you can use the following command:

sleep 1h

Conclusion

In this article, we explained how to use the Linux sleep command with examples. The sleep command is a useful tool for delaying the execution of a command or script for a specified amount of time. We hope you find this article helpful.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?