The “sleep” Command in Linux [10 Practical Examples]

The sleep command in Linux is used to set a delay in the execution of a command or to schedule a command to be executed after a specific time period. While running multiple commands in a sequence, the execution occurs immediately one after another simultaneously. However, there may be cases where it’s necessary to defer command execution to provide the system enough time to generate the desired outcome. In these cases, the sleep command plays a vital role. In this article, I will discuss the sleep command with some practical examples. So let’s start!

A. Description

The sleep command suppresses the execution of the next command for a certain amount of time, from the execution list of multiple commands. This command is necessary when the execution of the next command depends on the succession of the previous command. For example, while using the tee command or piping several commands. Besides, the sleep command can be used to set a time interval after which the command will execute itself, like an alarm clock. This property of sleep command is a perfect solution for Linux programmers as they write shell scripts to automate simple tasks where the delay in some command execution is mandatory.

B. Syntax

The sleep command is a simple yet essential command tool in Linux. The syntax of this command is plain. The syntax is,

sleep <number>[suffix]

NOTE: Remember that, in the above syntax there is no space between the number and suffix. Also, the suffix is enclosed by a square bracket which indicates that it is not mandatory.

Where number denotes simply the time (in seconds if no suffix is added ) and the available suffixes are,

  • s → seconds (by default)
  • m → minutes
  • h → hours
  • d → days

C. Options

The sleep command is a crucial tool for pausing between the execution of commands. It has only two command line options. Check the man page to read about the command.

man sleep

Useful Options

  • –help, to display the help manual.
  • –version, to display the command version information.

NOTE: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “sleep” Command in Linux

The sleep command has many practical usages. In the following article, I will talk about some of the basic usages of the command with practical examples. So keep reading!

Example 1: Delay Command Execution Using the “sleep” Command in Linux

The sleep command allows you to delay the execution of the command. The command with a specified number that indicates times in seconds (by default) simply delays execution by that many seconds. Also, you can modify the time by adding suffixes (s-seconds, m-minutes, h-hours, d-days) with the number. The syntax is,

sleep <number>

OR,

sleep <number>[suffix]

Follow the below steps to check practically.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sleep 10

➌ Now, press the ENTER button.

➍ Then, write the following command with added suffixes to the time:

sleep 1d 2h 5m 7s

➎ Finally, tap the ENTER key.

Output >

After running the first command, you will notice the second command prompt pops up after 10 seconds. The sleep command delayed the execution by 10 seconds.sleep command in linux. Then run the second command, and you can notice your command prompt isn’t loading. The sleep command has made the delay for over a day. Now if you want to cancel the delay period then just type CTRL+C and exit the sleep mode.sleep command in linux with added suffixes to the time.


Similar Readings


Example 2: Put Your Computer to Sleep Using the “sleep” Command in Linux

You can set your computer to sleep or hibernate after a certain period using the sleep command. Just set a delay time of execution for the systemct1 command and your computer will be set to sleep or hibernate after that period. The syntax is,

sleep <number> && systemct1 <funtion_name>

Here, function_name refers to suspend, hibernate, or shutdown.

NOTE: The “systemct1” command is used to manage and control the system and service manager. It provides a centralized and unified interface for managing systems such as stopping, starting, and restarting services. Also, it manages the system startup and shutdown process.

Go through the below steps to try it yourself.

Steps to Follow >

➊ Open the Terminal application on your Ubuntu.

➋ Type the following commands in the command prompt:

sleep 2h && systemct1 suspend

➌ Now, press the ENTER button.

Output >

After running the command, you will notice your computer will be asleep after 2 hours.sleep command in linux to put your computer in sleep mode.

Example 3: Run the “sleep” Command through Bash Shell Scripts

The sleep command is mainly used in shell scripts. Programmers use this command with other commands while writing codes to set a delay between them and then execute the script. You can write the sleep command in the shell script and then execute the script from your terminal. Make sure to add executable permission before executing the command.

Proceed to the below steps to write the sleep command on a bash script and then execute it.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt to open the nano editor:

nano

➌ Now, press the ENTER button.

➍ After that nano editor will pop up & write the following code in the editor,

#!/bin/bash 

echo "Begin $(date)"
sleep 10s 
echo "End $(date)"

➎ Write the script by typing CTRL+O & give the file name “Delay”  and exit by typing CTRL+X.

➏ Then execute the script by the following command:

./Delay

➐ Press the ENTER key.

➑ There will be an error as you don’t have the executable permission. Type the below command to give permission.

chmod +x Delay

➒  Press the ENTER button and then execute the script again by typing,

./Delay

➓ Finally, tap the ENTER key.

NOTE: Shebang ( #! ) is a special code in the first line of the script that specifies the interpreter to be used for executing the script. This line starts with “!#” followed by the path of the interpreter. Here, we used the bash interpreter located at /bin/bash.

Output >

First, open the nano editor and then write the command lines like the following picture. After that write out the script and save the file name as Delay.sleep command in bashscript. After that, execute the script from your terminal. But as you can see from the below picture there will be an error saying “permission denied”, as your script doesn’t have the executable permission. Then add the permission to the script using the chmod (Change mode) command with the x (execution) option. Later, run the script again and you will see the output of the script. There will be 5 seconds delay between the two dates, which was set by the sleep command.execute the sleep command in bashscript.

Example 4: Set an Alarm Using the “sleep” Command in Linux

One fun application of the sleep command is that you can use it to set an alarm. Just set a delay time with the sleep command to execute the mplayer command and then set a .mp3 file to ring after the execution. The syntax is,

sleep <time> && mplayer <.mp3_file_name>

Check the below steps to set an alarm with the sleep command.

Steps to Follow >

➊ Open the Ubuntu Terminal application.

➋ Type the following command in the c

sleep 7h && mplayer good-morning.mp3

➌ Now, press the ENTER button.

➍ After that, to see if the alarm works, type CTRL+C and then type the below command in the command prompt:

seep 2 && mplayer good-morning.mp3

➎ At last, press the ENTER key.

Output >

After running the first command, the output will look like the below image.set an alarm using sleep command in linux. To see if the alarm works run the second command, which will be executed within 2 seconds and you will hear your audio ringtone. Just like my player rang good-morning.mp3 file. Make sure to give the right .mp3 file name & also the location of the file, if it’s not in the current directory where your command prompt is running.set an alarm.

Example 5: Simulate a Digital Clock Using the “sleep” Command in Linux

To simulate a digital clock using the sleep command, just write a simple bash script with shebang as the first line and execute that script. After the execution, you will see a display clock on your terminal.

Go through the below steps to simulate a digital clock by yourself.

Steps to Follow >

➊ Start by opening the Ubuntu Terminal.

➋ Type the following command in the command prompt to open a nano editor with the file name clock:

nano clock

➌ Now, press the ENTER button.

➍ After that nano editor will pop up & write the following code in the editor,

#!/bin/bash 
while [ 1 ] 
do      
     clear      
     tput cup 5 30      
     date '+%r'      
     sleep 1 
done

Save the script by typing CTRL+S and exit by typing CTRL+X.

➏ Then execute the script by the following command:

./clock

➐ Finally, press the ENTER key.

Output >

Check the below image, after you write your code on the clock bash script, it will look like this.write the sleep command as bashscript in nano editor. After that, run the script just like the below image. Add permission if needed just like I did in Example 3.Execute the sleep command bashscript. After running the script, the digital clock will float on your terminal and it will look like the following image. You can close the clock anytime you want by pressing CTRL+C.Digital clock made by using the sleep command in Linux.

Example 6: Set a Delay Between the Execution of Two Commands Using the “sleep” Command in Linux

When executing multiple commands, you can set a delay between them. Suppose, you want to echo two words using the echo command. But you want to echo the second word 5 seconds after the execution of the first word. Then set 5 seconds delay between the two commands. The syntax is,

command 1 && sleep <time> && command 2

Read the below steps and practice accordingly.

Steps to Follow >

➊ Launch the Ubuntu Terminal.

➋ Type the following two commands in the command prompt:

sleep 5 && echo "Hiii" && sleep 5 && echo "there"
ls && sleep 5 && date

➌ Press the ENTER button after writing each command.

Output >

When I ran the first command, the output of the first echo “Hiii” came after 5 seconds as there was a 5 seconds sleep time set before it. Then the second echo output “there” came 5 seconds after the first echo because of the 5 seconds sleep of time between them.Delay between the execution of two commands. I ran the second command by putting 5 seconds of sleep time between the two commands ls and date. If you notice the output of the date command displayed 5 seconds after you saw the output of the ls command.Delay between the execution of two commands.

Example 7: Open a Website Using the “sleep” Command in Linux

You can use the sleep command with the CLI options for Firefox to open a website after a period of time. You can simply open any website or maybe you need to watch a live stream on youtube which you may forget about or wish someone on Twitter. Just give the website link and the time with the sleep command. It will tell your website browser to execute after that given time period and the website will pop up to remind you while you were busy watching a movie or doing something else. The syntax is,

sleep <time> && Firefox <website-link>

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt to open the linuxsimply.com website after 2 minutes:

sleep 2m && firefox https://linuxsimply.com

➌ Now, press the ENTER button.

Output >

Run the sleep command with your preferable time and desired website link. I set 2 minutes time to open the website linuxsimply.com, like the below picture.Open a website. When 2 minutes passed, the command was executed and my desired website opened just like the following image.Open a website.

Example 8: Check If a Host is Online with Intervals

Check if a host is online, using the sleep command. You can also set an interval of checking. To do so you have to write a bash script. In the script, write a code setting a while loop with the ping command and set delay with the sleep command between every iteration. Give the hostname as an argument after the ping command. The code will run in a loop with the specified delay until the host is online. When the host will be online the echo command will echo “online” and then break the loop.

Act according to the below steps to write the code yourself.

Steps to Follow >

➊ Begin by opening the Ubuntu Terminal.

➋ Type the following command in the command prompt to open a nano editor with the file name Host:

nano Host

➌ Now, press the ENTER button.

➍ After that nano editor will pop up & write the following code in the editor,

#!/bin/bash 

while : 
do     
     if ping -c 1 www.google.com &> /dev/null     then      
     echo "Online"
     break    
     fi 

     sleep 10
done

Save the script by typing CTRL+S and exit by typing CTRL+X.

➏ Then execute the script by the following command:

./Host

➐ Finally, hit the ENTER key.

Output >

Write the code in the nano editor, which will look like the below image after completion. Save the file name as Host and exit.write the sleep command as bashscript in nano editor. Run the script like the following image. Make sure to add permission if needed just like I did in Example 2. After that, you will see the output when the host will be online.Execute the sleep command bash script.

Example 9: Check the “sleep” Command Version on Your System

Check the version of the sleep command tool on your system using the option –version or -v of your command. The syntax is,

sleep --version

Read the below steps to see the command version on your system.

Steps to Follow >

➊ Run the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sleep --version

➌ Now, push the ENTER button on your keyboard.

Output >

The output display will show the Version, Copyright, and License of the sleep command.Check the version of the sleep command.

Example 10: Print the Help messages for the “sleep” Command

While working with the sleep command, if you need any help with the options and usage of the command use the –help option. It will concisely display all the details about the command options and then exit. The syntax is,

sleep --help

See the below steps to learn how to print help messages.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt:

sleep --help

➌ Finally, press the ENTER button.

Output >

The output will display the following help messages for the command.Display the help messages for the sleep command.

Conclusion

To conclude, the sleep command in Linux is a simple command tool with numerous roles. If you read the entire article, you already know it has many practical usages. Hope you liked reading about the command.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment