Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Linux sleep command

Reference for the Linux sleep command: pause for seconds, fractions, minutes, hours or days, combine suffixes, use it in retry loops and see why a sleep is often not the right fix.

sleep does nothing for a given amount of time and then exits. It is used in scripts to space out retries, to give a background process a moment to start, or to slow down a loop. While it sleeps it uses no CPU, so it is cheap to wait with.

The argument is a number followed by an optional unit. GNU sleep also accepts fractions and several arguments, which it adds together.

Terminal
sleep NUMBER[SUFFIX]...

Try it

Bash Durations, fractions and sums

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash A retry loop with a pause

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
sleep NUMBER[SUFFIX]...
sleep infinity
Suffix Unit
s (or none) Seconds
m Minutes
h Hours
d Days

NUMBER may be a decimal, such as 0.25 or 1.5m. With several arguments the total is the sum: sleep 1m 30s sleeps for 90 seconds. GNU coreutils also accepts infinity.

Returns: 0 after sleeping. On a bad argument (sleep abc) it prints sleep: invalid time interval 'abc' and returns 1. A signal such as SIGTERM ends it early.

Usage notes

  • Fractions and suffixes are GNU extensions. POSIX sleep takes only whole seconds; on macOS and BSD sleep 1m is an error, though fractions work on recent versions.
  • Not exact. sleep waits at least the time given; scheduling adds a few milliseconds, and the clock can pause when the machine suspends.
  • Polling is a smell. A fixed pause is a guess. Prefer to wait for the real condition: wait for a background job, a loop that checks for a file, or timeout around a check. Sleep inside such a loop only limits how often you check.
  • Signals. sleep is a separate process, so a trap in the parent runs only after sleep ends. In a loop that must react quickly, run sleep 60 & wait $!.
  • sleep infinity is handy to keep a container or a test process alive.
  • Not a rate limiter for parallel work: each process sleeps on its own.
  • Builtin alternative: Bash can sleep without forking using read -t 0.5 < /dev/zero on an unused descriptor, but plain sleep is clearer.