Nothing Special   »   [go: up one dir, main page]

Linux Sleep Command Explained (with Examples)

The “sleep” command in Linux helps in adding a specified delay in the execution of shell (bash) scripts. The command is very easy to use and has various powerful features. In this article, we will explain in detail how to use the sleep command in your shell scripts. The examples are based on Debian 10, but they will work on any other Linux Distribution.

Using the “sleep” Command in Linux

For using the “Sleep” command effectively in Debian 10, you are required to follow the following steps:

Launching the terminal in Debian 10 is the first step in this regard. For doing that, click on the Activities tab located on your desktop and type terminal in the search bar that appears. Now you need to click on the Terminal search result to launch the terminal in Debian 10. The newly opened terminal window is shown in the image below:

Linux Terminal

The best thing before using any unfamiliar command is to know the basic details about it. For doing that, you can make use of the “help” command with the following syntax and then press the Enter key:

sleep --help

This command is also shown in the following image:

Sleep command help

Running this command will show you all the necessary details about the “Sleep” command on your terminal as shown in the image below:

Online help for sleep command

You can also check the version of your “sleep” command at any time. For doing that, you will need to type the following command in your terminal and then press the Enter key:

sleep --version

This command is shown in the following image:

get command version

This command will display all the version related details of the “Sleep” command as shown in the image below:

Now we will proceed to the actual usage of the “Sleep” command. The regular syntax of the “Sleep” command is: sleep Num[suffix]. Here, Num is an integer or a floating-point number that specifies the delay whereas suffix can be "s", "m", "h", and "d" for seconds, minutes, hours, and days respectively. Second is the default suffix so you do not need to specify it explicitly. Therefore, if you want to induce a delay of some seconds before you get hold of the prompt again on your terminal, then you will need to type the following command followed by pressing the Enter key:

sleep Num

Here, replace Num with the seconds for which you want the “Sleep” command to be activated. In this example, I wanted to induce a delay of 2 seconds. Therefore, I have replaced Num with 2 as shown in the following image:

Delay command execution by 2 seconds

In the same manner, if you want to induce the delay of a few minutes, then you will need to type the following command and then press the Enter key:

sleep Num[m]

Here, replace Num with the minutes for which you want to induce the delay. In this example, I have replaced Num with 2 as shown in the image below:

Use Minutes instead of seconds in sleep command

Similarly, if you want to induce the delay of a few hours, then you will need to type the following command and then press the Enter key:

sleep Num[h]

Here, replace Num with the hours for which you want to induce the delay. In this example, I have replaced Num with 2 as shown in the following image:

Use hours in sleep command

Lastly, if you want to induce the delay of a few days, then you will need to type the following command and then press the Enter key:

sleep Num[d]

Here, replace Num with the days for which you want to induce the delay. In this example, I have replaced Num with 2 as shown in the image below:

Set delay in days

Another feature of the “sleep” command is that it can also be used to set up an alarm. In order for that to happen, you need to ensure that a sound is being played when the sleep timer expires. For doing that, you will need to type the following command and then press the Enter key:

sleep Num[suffix]; mediaplayer abc.xyz

You will need to follow the same pattern for the sleep command, however, you will need to replace mediaplayer with whichever media player you are actually using to play your sound file, abc with the file name and xyz with its actual extension. In this example, I wanted to play an mp3 file named music with the VLC player. Therefore, I have replaced mediaplayer with VLC, abc with music, and xyz with mp3 as shown in the following image:

Start a command with a delay

Now if you want to actually visualize the delay induced by the “Sleep” command, you can display certain messages on your terminal and note their time of appearance. For doing that, you will need to type the following command and then press the Enter key:

time (echo “Message before Sleep”; sleep Num[suffix]; echo “Message after Sleep”)

Here, you need to use the same convention as described above for the “Sleep” command whereas you can replace “Message before Sleep” and “Message after Sleep” with whatever messages you want to be displayed before and after the execution of the “Sleep” command. In this example, I wanted “End” to appear after two seconds of “Begin” therefore, I have made these replacements accordingly as shown in the image below:

Sleep command example

  • As soon as this command will execute successfully, you will be able to see the output shown in the following image:

Result of script

You can also use the “sleep” command in conjunction with other commands. In this example, I wanted to list the contents of a directory followed by the pathname of the current working directory. However, I want the path to appear after a delay. Therefore, I will type the following command and then press the Enter key:

ls && sleep Num[suffix] && pwd

Here, you will need to follow the same convention for the “sleep” command as described above and it is also shown in the image below:

another command example

As soon as this command will execute successfully, you will be able to see the output shown in the following image:

Command delayed yb 2 seconds

Conclusion

By following the method described in this article, you can conveniently master the usage of the “sleep” command in Debian 10.