Lesson 41: sleep Command
In this lesson, you'll learn how to use the sleep command to delay command execution for a specified amount of time in Linux.
The sleep command is used to delay the next command execution. It causes the calling program to sleep for a specified amount of time.
So, let's learn more about the sleep command with practical examples in Linux.
sleep Command Syntax
The syntax of the sleep command is very simple, as it accepts one mandatory parameter with an optional suffix:
$ sleep <NUMBER>[SUFFIX]
It is important to note that, in the above syntax, there isn't a space between the NUMBER and SUFFIX.
sleep Command Options
| Suffix | Description |
|---|---|
s |
Specify time units in seconds (default) |
m |
Specify time units in minutes |
h |
Specify time units in hours |
d |
Specify time units in days |
1. Delay Linux Command Execution
By default, the sleep command waits for a number of seconds. To understand this, let's print the current time before and after the sleep command.
For example, the following command waits for 5 seconds after printing the current time:
$ date '+%r'; sleep 5; date '+%r'
11:42:01 AM
11:42:06 AM
In this example, we have used a semicolon (;) to separate each command.
2. Make a Command Wait N Minutes
Optionally, the sleep command allows us to specify the time unit using suffixes.
So, let's use the m suffix to sleep for 1 minute:
$ date '+%r'; sleep 1m; date '+%r'
11:42:01 AM
11:43:01 AM
The important point to note is that there shouldn't be any space between NUMBER and SUFFIX.