Linux Sleep Command

Linux Sleep Command

The 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.

Basic Syntax of Sleep Command

The following syntax is mainly used for using the sleep command in the Linux system:

sleep NUMBER[SUFFIX]
  • When you use the sleep command, it starts with the keyword sleep, followed by the interval in numbers.
  • You can take the NUMBER as an integer or floating-point number.
  • The SUFFIX is 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

The 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.

sleep 0.5m

The above command will wait for 0.5 minutes.

sleep command m

In the following command, we used the h suffix with the sleep command. The time value is 0.005 hours.

sleep 0.005h

The above command will pause the terminal activity for 0.005 hours.

sleep command h

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 paused terminal activities

Sleep command can be used with other terminal commands. For example, we can use the sleep command between the timedatectl and 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 terminal commands

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

sleep command with loop

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 --help displays the information related to the sleep command.  The man sleep is also used to display the same helping details of the sleep command.
sleep --help
man sleep

sleep help witch

Sleep –version Switch

  • The --version switch 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:
sleep --version

sleep version switch

Conclusion

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.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like