Skip to content

sleep Command Cheat Sheet

sleep suspends execution for a specified amount of time. It is essential in scripts for polling, rate limiting, and waiting for resources.


Synopsis

sleep NUMBER[SUFFIX]...

Basic Usage

Seconds (Default)

Wait 5 seconds.

sleep 5

Suffixes

  • s: seconds (default)
  • m: minutes
  • h: hours
  • d: days
sleep 1m 30s
# Waits 1 minute AND 30 seconds (90s total)

Fractional Seconds

Most modern sleep implementations (GNU coreutils) support floating point numbers.

sleep 0.5
(Waits half a second).


Practical Examples

Build a Simple Alarm

sleep 10m && echo "Tea is ready!"

Polling Loop

Wait for a file to exist.

while [ ! -f lock.txt ]; do
  echo "Waiting for lock.txt..."
  sleep 1
done

Retry Logic

n=0
until [ $n -ge 5 ]; do
   command && break
   n=$((n+1)) 
   sleep 2  # Wait 2s before retry
done

Notes

  • Precision: While sleep accepts floats, it is not guaranteed to be real-time precise due to system scheduling latency.
  • CPU Usage: sleep uses practically zero CPU execution time. It puts the process in S (interruptible sleep) state.