Linux sleep command adds delay/pause for a specified amount of time. As per man sleep this command,
- Written by Jim Meyering and Paul Eggert.
- Part of GNU Coreutils.
- License GPLv3+: GNU GPL version 3
- free software, you are free to change and redistribute it. There is NO WARRANTY
Sleep Syntax
sleep NUMBER[smhd] sleep OPTION
Options
Each argument is a number followed by an optional unit.
The units are:-
- s – seconds
- m – minutes
- h – hours
- d – days
Note: The default unit is seconds.
–help – display this help and exit
–version – output version information and exit
An exit status of zero indicates success, and a nonzero value indicates failure.
Using Sleep Command (Examples)
sleep pauses for an amount of time specified in the argument. Just open the terminal and enter the below command.
sleep 5
You can see terminal sleep for 5 seconds and then respond back. The NUMBER argument to this command is mandatory while [smhd] units are optional. The default is seconds (s).
sleep sleep: missing operand Try 'sleep --help' for more information.
Historical implementations of sleep have required that number be an integer and only accepted a single argument without a suffix. However, GNU accepts arbitrary floating point numbers.
sleep 5.5
Sleep is shell built-in as well as an external command in some of the Linux distribution. Use type -a command to check this.
type -a sleep sleep is /bin/sleep
It is part of core utilities so use below to see additional information.
info coreutils 'sleep invocation'
Bash Script with Sleep Command (How to)
A shell script is a sequence of internal and/or external Unix commands. These are executed in sequence one by one. When you want to pause or add a delay in execution, you can use it as below.
Consider below bash sleep script. This script,
- Print message
- Wait for 5 seconds
- Print message
cat sleep_demo.sh
#!/bin/sh echo "Time before - " date sleep 5 date echo "Time after - "
Let’s run this Unix script using sh sleep_demo.sh command.
sh sleep_demo.sh
Time before - Sat Dec 15 12:28:06 IST 2018 Sat Dec 15 12:28:11 IST 2018 Time after -
You can clearly see the second message printed after 5 seconds.
This command is helpful when two programs running and you want one to wait for another for some time.
Let’s consider a simple interface where a system is Ftpeing a file to another system. In another system, there is scheduled cron job which reads and copy this file to some other location. Sometimes file creation may take time, in that case, you can add a sleep for some time and make another script until the file is created.
Below are examples of sleep commands with different options.
1. sleep for 1 second
sleep 1
or
sleep 1s
Note:- Entering unit is optional when you want to enter time in seconds.
2. sleep for 1 minute
Here, you have to use m unit as well, otherwise shell will assume it in seconds.
sleep 1m
3. sleep for 1 hour
Use h as unit for hour.
sleep 1h
4. sleep for a day
Use d as unit for day
sleep 1d
5. print help
sleep --help
6. print version information
sleep --version
Summary
The sleep command in Linux is a simple yet versatile utility that enables users to introduce controlled delays in their scripts or command sequences. Whether you’re automating tasks, debugging, or simply needing to pause between commands, sleep offers a straightforward approach to managing time intervals.
As we wrap up this tutorial, we hope you now feel confident in employing sleep to optimize and refine your Linux tasks.
Reference and further reading