Skip to content

The sleep Command in Linux: Pause a Script for Seconds, Minutes or Hours

3 min read

Applies to: Linux: GNU coreutils 9.7 (Debian 13), uutils coreutils 0.8.0 (Ubuntu 26.04) and BusyBox 1.37 (Alpine 3.24); tested on all three

sleep pauses for the time you give it, then exits. sleep 5 waits five seconds. Add a suffix for other units: s seconds, m minutes, h hours, d days. It is mostly used in shell scripts: to wait between retries, to space out requests or to wait for something to start.

Syntax and examples

sleep 5          # 5 seconds
sleep 0.5        # half a second
sleep 2m         # 2 minutes
sleep 1h         # 1 hour
sleep 1m 30s     # several values are added: 90 seconds
sleep infinity   # wait until the process is stopped

Fractions such as 0.5 and several values work in all three versions we tested. Only POSIX’s basic form, one whole number of seconds without a suffix, is guaranteed on every Unix system, so use that in scripts that must also run on other systems.

You can check the pause with time:

$ time sleep 1.5

real	0m1.507s
user	0m0.000s
sys	0m0.001s

sleep prints nothing and returns exit code 0 when the time is up. Press Ctrl+C to stop it early.

Error messages

$ sleep 5x
sleep: invalid time interval '5x'
Try 'sleep --help' for more information.
$ sleep
sleep: missing operand
Try 'sleep --help' for more information.

Both return exit code 1. Don’t put a space between the number and the unit: sleep 5 m fails with invalid time interval 'm', because m is read as a second value. BusyBox words the error as sleep: invalid number '5x'.

Use sleep in a script

Retry something a few times with a pause:

for i in 1 2 3; do
  echo "attempt $i"
  some_command && break
  sleep 10
done

Wait until a file appears (or a service answers), checking every second:

until [ -f /tmp/ready ]; do
  echo "waiting"
  sleep 1
done
echo "ready"

Run a check forever, every 30 seconds:

while true; do
  date >> /var/log/mycheck.log
  sleep 30
done

A random delay in cron

To stop many servers running the same cron job at exactly the same second, add a random wait of up to a minute before the command:

*/15 * * * * sleep $(shuf -i 0-59 -n 1); /usr/local/bin/backup.sh

Use shuf rather than $RANDOM here. Cron runs commands with sh, which is dash on Debian and Ubuntu, and dash has no $RANDOM: in our test $((RANDOM % 60)) always gave 0 in dash but a random number in Bash.

Related commands

  • timeout 10 some_command: stops a command if it runs longer than 10 seconds (exit code 124 when it had to stop it).
  • wait: waits until the background jobs started in the script (command &) are finished, instead of guessing a time with sleep.
  • read -t 10: waits up to 10 seconds for the user to press Enter, then carries on (Bash; the exit code is above 128 when the time ran out).

Which sleep you have

sleep is a separate program, not a Bash built-in (type sleep shows /usr/bin/sleep). Debian 13 uses GNU coreutils. Ubuntu 26.04 uses the Rust rewrite, uutils coreutils (sleep --version shows sleep (uutils coreutils) 0.8.0); the GNU version is still installed as gnusleep. Everything on this page behaved the same with both.

More scripting help: Bash functions, cron jobs and crontab syntax and running a command in the background.

Official documentation: GNU sleep and sleep(1).

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket