The sleep command in Bash pauses script execution for a specified duration, accepting positive integers or floating-point numbers as the time value. By default, the argument is treated as seconds, but you can append suffixes like m (minutes), h (hours), or d (days) to specify other units.
Syntax and Usage
The basic syntax is sleep NUMBER [SUFFIX] ..., where multiple arguments sum together to define the total wait time.
sleep 5pauses for 5 seconds.sleep 0.5pauses for half a second (supported by GNU/Linux versions).sleep 1h 30mpauses for 1 hour and 30 minutes.sleep infinitypauses indefinitely without consuming CPU, often used in Docker containers.
Practical Applications
Retries: Implement exponential backoff by doubling the delay after each failed attempt.
Countdowns: Create timers using a
forloop withsleep 1between iterations.Background Execution: Run
sleep 10 &to delay execution non-blocking, then usewaitto resume when finished.Interrupting: Press
Ctrl+Cto send aSIGINTsignal to a foreground process, orkill PIDfor background processes.
| Task | Command |
| Sleep for 5 seconds | sleep 5 |
| Sleep for 0.5 seconds | sleep 0.5 |
| Sleep for 2 minutes | sleep 2m |
| Sleep indefinitely | sleep infinity |
| Run in background | sleep 10 & |
Use the sleep command.
Example:
sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.
One can also employ decimals when specifying a time unit; e.g. sleep 1.5s
Use the sleep command.
Example:
sleep .5 # Waits 0.5 second.
sleep 5 # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.
One can also employ decimals when specifying a time unit; e.g. sleep 1.5s
For those looking for the Bash equivalent of Windows Powershell/CMD's pause command.
In Bash use read with option -p specifying a prompt like:
read -p "Press enter to continue"
shell script - Avoiding busy waiting in bash, without the sleep command - Unix & Linux Stack Exchange
Why bash scripts often have excessive 'sleep' commands
bash - How to do nothing forever in an elegant way? - Unix & Linux Stack Exchange
linux - Bash script how to sleep in new process then execute a command - Stack Overflow
Does `sleep` use CPU while waiting?
How do I cancel a `sleep` in a script?
Can `sleep` accept decimal values?
Videos
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ cd bash-3.2.48/examples/loadables
$ make sleep && mv sleep sleep.so
$ enable -f sleep.so sleep
Then:
$ which sleep
/usr/bin/sleep
$ builtin sleep
sleep: usage: sleep seconds[.fraction]
$ time (for f in `seq 1 10`; do builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative (sadly RHEL/CentOS 8 build bash-4.4 with loadable extensions deliberately removed), you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
As a last resort you could use perl (or any other scripting that you have to hand) with the caveat that initialising the interpreter may be comparable to the intended sleep time:
$ perl -e "select(undef,undef,undef,0.1);"
$ echo "after 100" | tclsh
The documentation for the sleep command from coreutils says:
Historical implementations of sleep have required that number be an integer, and only accepted a single argument without a suffix. However, GNU sleep accepts arbitrary floating point numbers. See Floating point.
Hence you can use sleep 0.1, sleep 1.0e-1 and similar arguments.
In newer versions of bash (at least v2), builtins may be loaded (via enable -f filename commandname) at runtime. A number of such loadable builtins is also distributed with the bash sources, and sleep is among them. Availability may differ from OS to OS (and even machine to machine), of course. For example, on openSUSE, these builtins are distributed via the package bash-loadables.
Creating a lot of subprocesses is a bad thing in an inner loop. Creating one sleep process per second is OK. There's nothing wrong with
while ! test_condition; do
sleep 1
done
If you really want to avoid the external process, you don't need to keep the fifo open.
my_tmpdir=$(mktemp -d)
trap 'rm -rf "$my_tmpdir"' 0
mkfifo "$my_tmpdir/f"
while ! test_condition; do
read -t 1 <>"$my_tmpdir/f"
done
Very often you see sleep 1 between commands where seemingly it is not required. For example this script which creates a new user:
mkdir /home/$newuser chown root:root /home/$newuser sleep 2 mkdir /home/$newuser/sftproot sleep 2 chown $newuser:$newuser /home/$newuser/sftproot
Is it just incorrect use of sleep or is there actually some benefit as opposed to just leaving it out?
In some cases it makes sense to wait (like async stuff) but shouldn't most of the programs be done with it as soon as they return something? Sometimes it is also used to give user a chance to see whats going on but for me the example above doesn't make any sense.
From the manpage:
Given two or more arguments, pause for the amount of time specified by the sum of their values.
So:
sleep 7m 30s
Another way to do this is:
sleep 7.5m
man sleep
Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number.
I don't understand why you would want to do this, but
for rotations in 1 2; do
for duration in 5 500 1000; do
sleep $duration
done
done
sleep is an external command - not a bash function.
To cycle indefinitely through a set of values, you can use an array with an index derived using modulo arithmetic. Ex.
#!/bin/bash
s=(5 500 1000)
i=0
while : ; do
# some commands
sleep "${s[i]}"
i=$((++i%3))
done
I don't think you're going to get any more elegant than the
tail -f /dev/null
that you already suggested (assuming this uses inotify internally, there should be no polling or wakeups, so other than being odd looking, it should be sufficient).
You need a utility that will run indefinitely, will keep its stdout open, but won't actually write anything to stdout, and won't exit when its stdin is closed. Something like yes actually writes to stdout. cat will exit when its stdin is closed (or whatever you re-direct into it is done). I think sleep 1000000000d might work, but the tail is clearly better. My Debian box has a tailf that shortens command slightly.
Taking a different tack, how about running the program under screen?
sleep infinity is the clearest solution I know of.
You can use infinity because sleep accepts a floating point number*, which may be decimal, hexadecimal, infinity, or NaN, according to man strtod.
* This isn't part of the POSIX standard, so isn't as portable as tail -f /dev/null. However, it is supported in GNU coreutils (Linux) and BSD (used on Mac) (apparently not supported on newer versions of Mac — see comments).
You can do
(sleep 30 && command ...)&
Using && is safer than ; because it ensures that command ... will run only if the sleep timer expires.
You can invoke another shell in the background and make it do what you want:
bash -c 'sleep 30; do-whatever-else' &
The default interval for sleep is in seconds, so the above would sleep for 30 seconds. You can specify other intervals like: 30m for 30 minutes, or 1h for 1 hour, or 3d for 3 days.