Where to start First steps track · 15 stages
Linux Labs

sleep — delay execution

4 min Ubuntu 24.04; Bash 5.2 for shell builtins

Pauses execution for a specified duration; does not test whether another program is ready.

Cheat sheet

5 commands Printable version
  • sleep 1 pause for one second
  • sleep 0.5 fractional seconds in GNU sleep
  • sleep 2s explicit time unit
  • sleep 1m one minute
  • sleep --help installed version help

Overview

sleep introduces a delay: the shell's next sequential step starts after it finishes. While waiting, the process normally sleeps and uses very little CPU. That makes it useful for practising process observation.

Waiting for time and waiting for a result solve different problems. Five seconds after starting a web server do not prove that it accepts requests. Readiness requires checking the service itself; completion of a child task calls for wait.

Options

2

The duration is an operand, not an option. GNU sleep accepts the suffixes s, m, h, and d; an unsuffixed number means seconds. Multiple intervals are added together. For a portable script, use one non-negative integer number of seconds.

Help and version options describe the program rather than start a timer. Check the available interface with sleep --help; do not assume that other implementations provide every GNU extension.

Option What it does Example
--help Show help for the installed version. sleep --help
--version Show the coreutils version. sleep --version

Nothing found. Try a shorter query.

Recipes

3

Separate two observations

bash
date
sleep 2
date

This provides a convenient pause between manual measurements. Scheduling may resume the process later than requested: this command is not a precision measuring instrument.

Start a short background task

Run the entire example in the same shell:

bash
sleep 3 &
delay_pid=$!
wait "$delay_pid"

$! saves the PID of the latest background launch. Here, wait connects continuation to the completion of a specific task instead of another guessed delay.

Interrupt a manual delay

Start sleep 30 in the foreground and press Ctrl+C. In an ordinary terminal this interrupts the delay. You do not need to repeat the example with a long interval to verify it.

Troubleshooting

2

The terminal has not displayed a prompt

The foreground command is still running. Wait for it or interrupt it; silence during a delay is normal. It does not by itself mean that the shell is stuck.

An error appears instead of a delay

Check the operand: a time suffix belongs next to its number. Start with sleep 1, then compare the failing form with the installed program's help.

Danger zone

A delay does not limit how long a neighbouring command can run and does not replace its timeout. In an endless loop it reduces repetition frequency but provides no exit condition.

Do not terminate every process named sleep to clean up an example: somebody else's delay may belong to a working script. Use the saved PID and verify that it still identifies the intended instance.

See also

  • background-jobs — background jobs in the current shell.
  • wait — waiting for a child task to finish.
  • processes — states, PIDs, and signals.
Loading…