19.5 C
New York
Wednesday, June 29, 2022
HomeLinuxLinux CommandsHow To Use Sleep Command in Linux: Explained with Examples

How To Use Sleep Command in Linux: Explained with Examples

Linux operating systems are all about commands and the way you use them to complete your task. Sleep command in Linux is one of the commands you can use to delay a specific time during a script’s execution process. It benefits the developers when they want to pause the command execution for one particular time. So if you also want to learn everything about sleep commands, this article is just for you. In this article, you will learn about every possible detail of the sleep command in Linux.

Sleep command in Linux


You can easily use the sleep command for setting up a delay amount by days (d), hours (h), minutes (m), and seconds (s). Here is how it works:

sleep NUMBER[SUFFIX]

At the place of the suffix, you can use any time value (integer or fractional number). In case you don’t use any number instead of a suffix, then the system will automatically calculate a few seconds by default. 

The sleep command also allows different values that you can easily add together to evaluate the sleep duration. In case you want to stop the sleep process, then press CTRL and C keys simultaneously. For the help option, execute the following command:

sleep --help

Command to check help

You can view version details by typing:

sleep --version

Command to check Sleep Version

Practical Linux Sleep Command for Everyday Use


We will now go through some important and valuable examples so that you can understand everything better. 

1. Basic Example


Here we want to use the sleep command for 5 seconds. That’s why we will execute the following command in the terminal: 

sleep 5

Sleep Command Seconds

If you want to use minutes or hours instead of seconds, you can convert the seconds into minutes or hours accordingly. 

sleep 0.0833m

Command to check in minutes

sleep 0.0013889h

Command to set sleep in hours

In case you want to use the sleep command for specific minutes and seconds, then please use the below command: 

sleep 1m 2s

Set the sleep in minutes and seconds

2. Set the Alarm 


You can also set the alarm with a particular alarm sound. For example, we want to set the alarm, so we will execute the following command to play the “alarm_song.mp3” song after 8 hours and 15 minutes:

sleep 8h 15m && alarm_song.mp3

Set an alarm with sleep command

3. Delay Commands Using Sleep


You can use the sleep command to enforce a specific time between the execution of two different tasks (commands). Here is the following example in which we will play two songs one by one:

sleep 5 && echo "Play Song 1" && sleep 5 && echo "Play Song 2"

Delay Commands Using Sleep

We want to execute two commands one by one but with a gap of 10 seconds. Here we will execute the ls command to check the files in the system and then du -h to check the total size of the directories in the human-readable format:

ls && sleep 10 && du -h

Delay between two commands

4. Sleep Command in a Script


For example, we will use the sleep command to loop a specific task in the script. Here, we want to change the value of “n” from 1 to 7 after every second, so we have created “script.sh” with the following details:

Sleep Command in a Script

Now, we will execute the script in the terminal (we have saved the “script.sh” file in the Documents, so we have executed cd ~/Documents and ls commands):

bash script.sh

Script in Loop using sleep

Finally, Insights


The sleep command is useful when you need to perform more than one command in a bash script because some commands’ output may take a long time to process, and other commands need to wait for the previous command to complete entirely. It is impossible to start the next download before completing the previous one, for example, when you want to download sequential files. In this case, the sleep command is preferable as it will wait for a specific amount of time before each download.

Jahid Onik
Jahid Onik
He has completed his Electrical and Electronic Engineering graduation from a well-reputed University. He has been working as a data analyst for the past few years. He has been using Ubuntu since 2014.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Post

Must Read