sleep command is useful for delaying a script for a specified amount of time. In Linux or Unix-like operating systems, most programmers use the sleep command when they need to stop the execution of a particular command or bash script after the specified number of intervals.
By default, it takes intervals in seconds but, we can also use the small suffix such as seconds (s), minutes (h), days (d) that will further convert into other formats. The delay amount with the sleep command is specified by a number.
In this article, we’ll demonstrate how to use the Linux sleep command on our system with the help of various examples.
Table of Contents
Basic Syntax of Sleep Command
The following syntax is mainly used for using the sleep command in the Linux system:
- When you use the sleep command, it starts with the keyword
sleep, followed by the interval in numbers.
- You can take the
NUMBERas an integer or floating-point number.
SUFFIXis the unit of measure. The suffix is optional in the above command. If you will not define any suffix, it will calculate the time delay value by default in seconds (s). You can use the suffix as seconds (s), minutes (m), hours (h), and days (d).
Use of Sleep Command – Examples
sleep command pauses for a specified time interval. This command is usually used in the script but, you can also use the sleep command to pause a command. In the following section, we will elaborate on the use of sleep commands with the help of some examples.
Use Sleep command Without Suffix
You can also use the sleep command without specifying any suffix. In this way, it will take the time limit in seconds by default.
In the following example, we have used the sleep command numeric value 4 without any suffix. This script will print the
Welcome to the sleep command guide after 4 seconds and it displays the different types of time values such as real, user, system. Create a text file and paste your bash script code inside the file.
#!/bin/bash echo "Waiting for 4 seconds..." sleep 4 echo "Welcome to the sleep command guide"
Run the above script by using the time command in the following way:
time bash sleepcmd.sh
Use Sleep Command with Suffix
You can use the sleep command with suffix seconds (s), minutes (m), hours (h), and days (d). In the following example, we used the minute suffix with sleep command for 0.05 m.
The above command will wait for 0.5 minutes.
In the following command, we used the h suffix with the sleep command. The time value is 0.005 hours.
The above command will pause the terminal activity for 0.005 hours.
You can also use the multiple suffixes with the sleep command. For example, pause the terminal user prompt for 0.5 minutes and 10 seconds:
sleep 0.5m 10s
Pause Terminal Commands Using Sleep
Using the sleep command, you can pause the terminal activities on your Linux system. To do this, use the following command:
cd Music && sleep 20 && ls
The above command will change the directory and sleep terminal for 20 seconds. After that, it will list all files of the Music directory as follows:
Sleep command can be used with other terminal commands. For example, we can use the sleep command between the
date commands as follows:
timedatectl; echo “wait for 10 seconds…”; sleep 10; date
First, when you run the above command it will display the current timezone information of your system and echo a message
wait for 10 seconds... After that,
sleep 10 waits for the terminal for 10 seconds and then displays the current system date and time.
Sleep Command With Loop
Mostly you use the sleep command for pausing scripts. Therefore, you can use the sleep command with a while loop to do a task periodically after a specified time. For example, we want to see the system memory usage information after every 10 seconds. In this case, you can use the following bash script:
#!/bin/bash while true do Free sleep 10 done
Now, save the above text file with
resource.sh. You can keep the desired name of this file. Run the script using the following command:
time bash resource.sh
After running the above script, you will notice the system resource memory information will be updated after every 10 seconds on your terminal screen. Press
Ctrl+c to quit this script.
Sleep Command Switches – (sleep --help ,sleep --version)
The sleep command has the following switches or options:
Sleep --help switch
- The option
--helpdisplays the information related to the sleep command. The
man sleepis also used to display the same helping details of the sleep command.
Sleep –version Switch
--versionswitch or option of sleep command displays the sleep command version number, license, copyright information, and authors names. You can display all these details using the following syntax:
We have demonstrated the different uses of the sleep command in this tutorial. The sleep command is very helpful to execute a bash script along with various Linux commands to control your system tasks. When you need to perform a long interval-based job for a large number of files, in this case, the sleep command provides an ease to you.
For example, we want to batch files sequentially, one after another. We can use the sleep command in a bash script that displays a completion message with the file name after each download.