How to use the sleep command in Linux to pause scripts

How to use the sleep command in Linux to pause scripts

With `sleep 30` running in the foreground, Ctrl+C can cut the wait short. I’m fascinated that a keypress can interrupt a running command without changing the rest of the computer’s power state.

That matters when choosing between a timer and waiting for a child process to end. The command’s scope starts with the process `sleep` pauses.

What the sleep command pauses

The Linux sleep command delays the process that runs it for a chosen duration, not the computer’s power state.

ActionWhat pauses
sleep commandThe calling process waits for a duration or an interrupting signal.
System suspendThe computer changes its power state.
Shell wait commandThe script waits for a child process to finish.

That distinction helps choose the right operation. Use sleep for a fixed delay, and use the shell’s wait command when the next action depends on another process finishing.

Check which sleep implementation you have

GNU Coreutils sleep accepts a number of seconds by default, optional unit suffixes, fractional values, and multiple duration arguments. A Linux system can provide a different implementation, so check its command before relying on GNU-specific syntax.

command -v sleep
sleep --version

The version option is supported by GNU sleep. If your implementation does not accept it, read its local manual instead. The guide to Linux shell types can help you identify which shell runs your script.

Use sleep in a shell script

Run sleep on its own to delay the next command. The following message appears only after the short wait returns.

Pause before the next command

sleep 0.2s && printf 'The next command ran\n'
Terminal output after a short sleep command
The following command runs after the delay ends.

I ran this with a 0.2-second delay, and the message printed after sleep returned. The && operator runs printf only when the delay command succeeds.

Add a fractional or combined delay

GNU sleep accepts fractional seconds and adds the duration of each argument. The units are s for seconds, m for minutes, h for hours, and d for days. Without a suffix, a value means seconds.

sleep 0.1s 0.2s && printf 'Combined delay finished\n'

This command adds the two durations into a single wait, then prints the message. I ran this exact command, and it completed with the output shown above.

For a pause between repeated tasks, place sleep inside a Bash for loop.

Put the delay between script commands

Save this as takeabreak.sh. The Linux shell-script run instructions show how to make it executable.

#!/usr/bin/env bash
printf 'Time For A Break\n'
sleep 10s
printf 'I Am Back\n'

Use the shell’s time command to include the delay in the reported elapsed runtime.

chmod +x takeabreak.sh
time ./takeabreak.sh

The terminal screenshot shows both messages and the elapsed time measured with the shell’s time keyword. I ran the script, and the elapsed time also includes its commands.

Terminal run showing a Bash script's two messages and about ten seconds of elapsed time
The script prints both messages around its ten-second delay.

The elapsed value is not an exact timer guarantee. The time command guide explains how to read a command’s total runtime.

Know where a sleep delay stops helping

GNU-specific syntax is not the portable POSIX form. A portable script passes one non-negative integer without a suffix, while GNU sleep also accepts fractional values and multiple arguments.

SituationWhat to use
GNU duration with units or fractionsUse GNU sleep syntax after confirming the implementation.
Portable POSIX shell scriptUse one non-negative integer number of seconds without a suffix.
Wait for a background child processUse the shell wait command rather than guessing a delay.
Stop a foreground sleepPress Ctrl+C to send SIGINT to the foreground job.

For a background sleep, keep its process ID and send it a signal when the delay should end. I ran this cleanup example to confirm the child process was stopped and reaped.

sleep 30 &
sleep_pid=$!
kill "$sleep_pid"
wait "$sleep_pid" 2>/dev/null || :

The wait command returns a nonzero status when the child was terminated, so the final || : keeps that expected status from stopping a script that uses errexit. If you need repeated condition checks rather than a fixed delay, see the guide to a Bash while loop.

Wait for a condition when timing varies

A fixed delay does not tell you whether a service or child process is ready. If the next action depends on its state, check that state and use sleep only to space repeated checks, such as in a Bash for loop when each pass needs a pause.

Use time to measure a fixed wait, but let the condition decide when work can continue.

time sleep 1

Sleep command questions

How do I stop the sleep command in Linux?

Press Ctrl+C while sleep runs in the foreground. For a background process, save its PID with $! and send it a signal with kill.

Can sleep pause a script for less than one second?

GNU Coreutils sleep accepts fractional seconds, such as 0.2 seconds. The scheduler can resume the process later than requested, so a decimal does not promise a precise wake-up time.

Does the sleep command put Linux to sleep?

No. It delays the process that calls it. System suspend changes the computer’s power state and uses a different system action.