How to Use the Linux sleep Command with Examples

January 16, 2025

Introduction

When a user runs a sequence of commands in Linux, the commands execute either one after the other or concurrently. 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 terminal.

What Does the Linux sleep Command Do?

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

It accepts time intervals in seconds, minutes, hours, or days, allowing flexible delays. By integrating sleep into scripts, users control the timing of automated tasks and manage system resources.

Linux sleep Command Syntax

The sleep command syntax is:

sleep [number]

For example, to delay the second command prompt for five seconds, enter:

sleep 5
sleep 5 terminal output

By default, the system reads the number after sleep as the number of seconds.

 Linux sleep Command Options

Linux sleep command only has two options:

  • -h, --help. Displays the help message with usage instructions and exits.
  • -v, --version. Outputs the command version information and exits.

However, it supports time units such as:

  • s. Seconds.
  • m . Minutes.
  • h. Hours.
  • d. Days.

To specify time units, use the following syntax:

sleep [number][unit]

For example, to specify the delay time of two minutes and five seconds, run:

sleep 2m 5s
sleep 2m 5s terminal output

Additionally, set multiple durations by providing them as separate arguments, which are added together to determine the total delay.

The sleep command also supports floating-point numbers, which include a decimal point (e.g., 1.5 or 0.25).

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

Linux sleep Command Examples

The sleep command is used to control the timing of automated tasks, manage delays between commands, or ensure sufficient time for a process to complete before proceeding with the next operation.

The following sections contain examples of uses of 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 schedule the system to play an mp3 file after a specified time. In this example, the command uses mplayer to play alarm.mp3 after a seven-hour and 30-minute delay:

sleep 7h 30m && mplayer alarm.mp3
sleep 7h 30m && mplayer alarm.mp3 terminal output

After the specified time, the system plays the file:

playing alarm.mp3 terminal output

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"
sleep 1 && echo "one" && sleep 1 && echo "two" terminal output

Assign a Variable to the sleep Command

It is possible to assign a variable to specify the sleep command duration. To do that, create an example shell script. Take the following steps:

1. Use a text editor like Vim to create a new script file. For example, type the following command:

vim sleep_script.sh

2. Copy and paste the following 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}"
sleep_script in Vim terminal output

In this script, the variable SLEEP_INTERVAL has the value 30, which is then used as an argument for the sleep command. The script calculates and displays the time before and after sleep, showing that the execution has been paused for 30 seconds.

3. Save and exit the file.

4. Run chmod to make the script executable:

chmod +x sleep_script.sh

The command has no output.

5. Execute the script with:

./sleep_script.sh
./sleep_script.sh terminal output

When you run the script, it displays the time before the sleep command. After pausing for 30 seconds, it shows the time after the sleep command, displaying the delay.

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 continuously attempts to ping the website, and if the ping is successful, it stops. If the ping fails, the script waits 10 seconds before trying again.

To accomplish that, take the following steps:

1. Create a new script file in a text editor of choice:

vim check_website.sh

2. Paste the following content:

#!/bin/bash
while :
    do
        if ping -c 1 www.google.com &> /dev/null
        then
        echo "Google is online"
        break
        fi
    sleep 10
done
check_website script in Vim terminal output

The script continuously pings www.google.com until it receives a successful response. If the ping is successful, it prints Google is online and exits the loop. If the ping fails, it waits 10 seconds before retrying.

3. Save the file and exit.

4. Run the following command to make the script executable:

chmod +x check_website.sh

The command has no output.

5. Execute the script with:

./check_website.sh
./check_website.sh terminal output

Allow Time for Operation Completion

Use sleep to manage a bash script that internally calls two other bash scripts – one that runs tests in the background and another that prints the results. To ensure the second script doesn't print incorrect results before the first script has finished, run sleep to introduce a delay between checks.

Take the following steps to do that:

1. Create a script in a text editor:

vim wait_for_process.sh

2. Paste the following content:

while kill -0 $BACK_PID ; do
    echo "Waiting for the process to end"
    sleep 1
done
wait_for_process.sh in Vim terminal output

Note: Replace $BACK_PID with the actual process ID of the background process you want to monitor for the script to work.

The script uses the kill -0 command to check if the process with the ID stored in $BACK_PID is still running. If the process is running, it prints a message, sleeps for one second, and checks again until the process finishes.

3. Save and exit the file.

4. Make the script executable with:

chmod +x wait_for_process.sh

The command has no output.

5. Run the script with:

./wait_for_process.sh

The output is the following:

  • "Waiting for the process to end" repeatedly if the process is running.
  • "Process finished" if the process finishes.
  • "Process not found!" if the process finishes.

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.

Take the following steps:

1. Create a script file with a text editor:

vim predict_latency.sh

2. Paste the following content:

for (( i = 1 ; i <= 250 ; i++ )); 
    do  
    sleep 1
    qsub computation"${i}".pbs
done
predict_latency script in Vim terminal output

The script loops 250 times, increasing i (a variable used as the loop counter) by one each time. It waits one second between each loop and submits a job with a file name based on i.

3. Save and exit the file.

4. Make the script executable with:

chmod +x predict_latency.sh

The command has no output.

5. Run the script with:

./predict_latency.sh

If everything works correctly, there is no output until the job scheduler processes the jobs. After submitting all 250 jobs, the script completes silently, with a one-second delay between each submission.

Conclusion

This tutorial explained how to use the Linux sleep command to pause the execution of the commands in a sequence.
To learn how to wait for background running processes to complete and return the exit status, read about the bash wait command.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use the Linux tee Command
December 3, 2024

By default in Linux, a command received through standard input writes directly to standard output. The tee...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
Linux Ping Command Tutorial with Examples
April 2, 2024

Most Linux users are familiar with the ping command and know how to use it in its basic form. However, there...
Read more
How to Check CPU Utilization in Linux with Command Line
March 6, 2024

You have probably noticed your Linux OS slowing down, especially when working harder. Understanding CPU...
Read more