sleep Command in Linux: Pause a Bash Script

By 

Updated on

6 min read

Bash Sleep

The sleep command in bash pauses the execution of a script for a specified amount of time. It is one of the most common commands used in shell scripting, especially when you want to retry a failed operation, rate-limit a series of requests, or add a small delay inside a loop.

In this guide, we will show you how to use the sleep command with practical examples, including how to sleep for fractional seconds, how to combine multiple time units, and how to run sleep in the background.

Syntax

The syntax for the sleep command is as follows:

txt
sleep NUMBER[SUFFIX]...

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.

Sleep Command Examples

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

Sleep for Seconds

Terminal
sleep 5

Sleep for Fractional Seconds

Terminal
sleep 0.5

Sleep for Multiple Durations

You can combine multiple values. The following pauses for 2 minutes and 30 seconds:

Terminal
sleep 2m 30s

Sleep Indefinitely

To pause a process indefinitely until it is manually interrupted, use infinity:

Terminal
sleep infinity

This is commonly used in Docker containers and systemd services to keep a process running without consuming CPU.

Script Examples

The following examples show how sleep is commonly used in bash scripts.

Timing a Delay

This script prints the time before and after a 5-second delay:

sh
#!/bin/bash

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

# sleep for 5 seconds
sleep 5

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

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 prints the current time.

output
13:34:40
13:34:45

The output shows a 5-second gap between the first and second timestamps, which confirms that sleep 5 paused the script before the final date command ran.

Waiting for a Host to Come Online

The following script checks whether a host is online every 5 seconds and notifies you when it becomes reachable:

sh
#!/bin/bash

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

How the script works:

  • The while loop runs indefinitely using : as the condition.
  • The ping command sends a single packet to the target host.
  • If the host is reachable, the script prints “Host is online” and exits the loop with break.
  • If the host is not reachable, sleep 5 pauses the script for 5 seconds before retrying.

Retry with Exponential Backoff

When retrying a failed operation, it is good practice to increase the delay between attempts. This avoids overwhelming the target service:

sh
#!/bin/bash

max_retries=5
delay=1

for ((i = 1; i <= max_retries; i++)); do
  if curl -s --fail https://example.com > /dev/null; then
    echo "Request succeeded on attempt $i"
    exit 0
  fi

  echo "Attempt $i failed. Retrying in ${delay}s..."
  sleep "$delay"
  delay=$((delay * 2))
done

echo "All $max_retries attempts failed."
exit 1

The delay starts at 1 second and doubles after each failure: 1s, 2s, 4s, 8s, 16s.

Countdown Timer

A simple countdown timer that prints the remaining seconds:

sh
#!/bin/bash

seconds=${1:-10}

for ((i = seconds; i > 0; i--)); do
  printf "\rTime remaining: %d seconds " "$i"
  sleep 1
done

printf "\rTime's up!                    \n"

Run the script with an optional number of seconds:

Terminal
bash countdown.sh 30

Running Sleep in the Background

You can run sleep in the background to create non-blocking delays:

Terminal
sleep 10 &
echo "Sleep is running in the background with PID $!"

The & operator sends the process to the background. The special variable $! holds the PID of the last background process.

To wait for a background sleep to finish, use the wait command :

Terminal
sleep 10 &
pid=$!
echo "Doing other work..."
wait "$pid"
echo "Sleep finished"

Pause Until a Keypress

In some scripts, you may want to pause until the user presses a key instead of waiting for a fixed amount of time. The sleep command cannot do this on its own, but you can combine it with the read command :

sh
#!/bin/bash

read -n 1 -s -r -p "Press any key to continue..."
echo

The -n 1 option tells read to accept a single character, -s hides the keypress from the terminal, and -r prevents backslash interpretation. This is a common way to make a script wait for confirmation before continuing.

Interrupting Sleep

To interrupt a running sleep command, press Ctrl+C. This sends a SIGINT signal that terminates the process.

To terminate a background sleep process, use kill:

Terminal
sleep 300 &
kill $!

Quick Reference

TaskCommand
Sleep for 5 secondssleep 5
Sleep for 0.5 secondssleep 0.5
Sleep for 2 minutessleep 2m
Sleep for 1 hour 30 minutessleep 1h 30m
Sleep indefinitelysleep infinity
Sleep in backgroundsleep 10 &
Interrupt sleepCtrl+C or kill PID

FAQ

Is sleep a bash builtin?
No. The sleep command is provided by the GNU coreutils package and lives at /usr/bin/sleep. Because it is an external program rather than a shell builtin, the GNU version supports features like fractional seconds and the infinity argument that are not part of the POSIX specification.

Can sleep accept decimal values?
Yes. The GNU version of sleep used on Linux supports floating-point numbers. For example, sleep 0.5 pauses for half a second. The POSIX specification only requires integer support, so this will not work on every Unix system.

How do I wait for user input with a timeout?
Use read -t SECONDS instead of sleep. For example, read -t 5 -p "Enter your name: " name waits up to 5 seconds for input and continues automatically if the user does not type anything in time.

What is sleep infinity used for?
sleep infinity pauses a process indefinitely without consuming CPU. It is commonly used in Docker containers to keep the container running, or in scripts that need to wait for a signal.

Does sleep use CPU while waiting?
No. The sleep command suspends the process and does not consume CPU cycles during the wait period.

Can I combine multiple time values?
Yes. sleep 1h 30m 10s sleeps for 1 hour, 30 minutes, and 10 seconds. The values are added together.

How do I cancel a sleep in a script?
Press Ctrl+C to send SIGINT to the foreground process. For background sleep processes, use kill PID where PID is the process ID returned by $!.

Conclusion

The sleep command pauses the execution of the next command for a given amount of time. It is commonly used in shell scripts for retrying operations, rate limiting, and scheduling delays.

For longer scripts, keep delays explicit and pair them with clear output so you can tell whether the script is waiting, retrying, or finished.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page