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:
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:
You can view version details by typing:
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:
If you want to use minutes or hours instead of seconds, you can convert the seconds into minutes or hours accordingly.
In case you want to use the sleep command for specific minutes and seconds, then please use the below command:
sleep 1m 2s
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
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"
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
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:
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):
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.