In this tutorial, we’ll learn about the sleep command in Linux. While working with bash scripts on your Linux system, some situations may require you to delay the execution of a script. In this case, you need to use the sleep command. This tutorial will help you understand and learn to use the sleep command in Linux to pause scripts.
Basic Usage of the sleep command in Linux
Programmers working on a bash script commonly use the sleep command in Linux. The sleep command, as the name suggests, tells the processor to take a break. This causes the execution of the next command to be delayed by a specified duration.
The syntax of the sleep command is as follows:
Here, ‘n’ specifies the number of said intervals of time required to pass before the next command is executed while the ‘suffix’ specifies the time interval. When we use the sleep command in Linux to pause scripts, it allows for four different suffixes. They are:
- s: Sets the time interval as seconds
- m: Sets the time interval as minutes
- h: Sets the time interval as hours
- d: Sets the time interval as days
By default, the sleep command sets the time interval as seconds. If we don’t add a suffix while using the sleep command in Linux to pause scripts, the next command will be executed after N seconds. When you use two or more suffixes in a single sleep command, the delay before the execution of the next command is equal to the total sum of values of time specified.
Using sleep Command in Linux to Pause Scripts
The previous section taught us what the sleep command is, what its syntax is and what the meaning of the suffixes is. Now its time to use it in our scripts. Let us discuss the basics of each with the help of examples.
Sleep for ‘n’ seconds
Sometimes you need to pause your script for only a short amount of time. Let’s say you wish to pause a script for 10 seconds. This can be done using the ‘s’ suffix. Your sleep statement should look as follows:
Alternatively, you can decide not to mention the suffix in the command. Note that this only works when we are dealing with some number of ‘seconds’ of sleep. This exception works because ‘s’ is the default suffix for the sleep command. An example of this is:
Sleep for hours, minutes, and days, using the Linux sleep Command
When working with tasks that require a longer time interval between each execution, you need to specify the suffix after each number.
sleep 10m # for 10 minutes sleep 10h # for 10 hours sleep 1d # for 1 day
Combining Time Variables While Working with the sleep Command in Linux
While using the sleep command in Linux, you aren’t forced to use only one suffix at a time. If you wish, you can use two or even all suffixes at the same time. The total time that your script is paused will be equal to the sum of values for each suffix. This would look like the following:
sleep 6d 9h 4m 1s
This sleep command will pause a script for 6 days 9 hours 4 minutes and 1 second. Adding the suffix ‘s’ is optional even in this case.
Splitting Time in Fractions – Using Floating Integers
The number before a suffix doesn’t need to be a whole number. The sleep command allows for use of floating-point numbers before the suffix. This means that even though the smallest suffix represents a second, we can use the sleep command in Linux to pause scripts for way less than a second.
Here is how you can use the sleep command in Linux to pause scripts for a tenth of a second:
Further, you can also use floating-point numbers in front of any suffix, which allows us to easily specify the amount of time we need to write in the sleep command in Linux to pause scripts. An example is:
This command will pause the script for 1 hour and 30 minutes.
How to Use the Sleep Command in Linux Shell Scripts?
We saw how we can use the sleep command in Linux to pause scripts for a specified amount of time. But how does it look when we use this command in a bash script? Here is an example where we see the sleep command in action.
First, we create a simple bash script containing a 10s pause using the sleep command. it should look like this:
#!/bin/bash echo "Break time!!" sleep 10s echo "Back to work!"
When we use the time command to find the time taken by the script to be executed, we see that it runs for slightly longer than the delay time of 10s. The extra time is just the time when the script was being executed, without the delay.
This tutorial was aimed to help you learn how the Linux sleep command works and how you can use this command to pause a script. We hope now you understand how to use sleep command in Linux. It’s time to try it out for yourself. If you want to learn more about shell scripts, begin with our shell scripting 101 series.