Linux sleep Command | Explained With Examples

In Linux, the sleep command adds a delay for running the commands or scripts. When the user wants to execute a command after a specific time, you can add a delay it can be seconds (s), minutes (m), or hours. The sleep command is very useful when you want to complete the previous commands first and then execute the new commands. The post’s content is as follows:

Let’s get started!

What is Linux sleep Command?

The sleep command pauses the system to execute the previous execution and then run the next command. This command sleeps the system for a specific time or at a specific time. This article will discuss the different uses of sleep command. Let’s start!

Syntax of sleep Command

How to install Kali Linux in Window...

We can use the sleep command differently. Check the syntax for the sleep command below:

$ sleep number[suffix]...

The syntax components are described below:

  • The “number” represents the numerical value for the delay.
  • The “suffix” can be second (s), minutes (m), hours (h), or days (d). If the suffix is not provided, it will take seconds as the default value.

For more help on the sleep command, use the following command:

$ sleep --help

Let’s get into the usage of the sleep command!

How to Use sleep Command in Linux?

The sleep command can be utilized in several ways, which are elaborated with the help of different examples.

Example 1: How to Delay for Specific Time?

We can add a delay of seconds, minutes, hours, and days in Linux with the sleep command. Let’s discuss how to add a specific delay.

Delay for Second

To delay three seconds in the execution, use the below sleep commands with the specified number of seconds:

$ sleep 3

We can use the sleep command with the above syntax with any other command to delay the system for a specific time. For instance, to sleep the system for three minutes with the cat command is given below:

$ sleep 3 && cat testfile3.txt

Delay for Minutes

To pause the execution for minutes, the “m” convention is used. To delay for three minutes, use the below-written command:

$ sleep 3m

Note: You can use the floating number like 0.5 hours for 30 minutes.

For pausing the system to run the “pwd” command for three minutes, execute the below command:

$ sleep 3m && pwd

Delay for Hours

For sleeping the system for three hours (h), utilize the below command:

$ sleep 3h

If you want to run the command “cd ~/Downloads”, after five hours, run the below command in the terminal:

$ sleep 3h && cd ~/Downloads

Delay for Days

To delay the execution of the system for three days, use the following command:

$ sleep 3d

If you want to play an audio  testfile.mp3 after/schedule three days, execute the following command:

$ sleep 3d && testfile.mp3

Delay for Specific Hours and Minutes

To pause the system for specific seconds, minutes, hours, and days, we can use them together. For instance, to sleep the system for two hours and two minutes, utilize the below command utility:

$ sleep 2h 2m

For example, to have a pause of 2 hours and 2 minutes to play a video named testfile.mp4, execute the following command:

$ sleep 2h 2m && testfile.mp4

Example 2: How to Delay the Multiple Linux Commands?

The sleep command allows you to give a pause between two commands. For example, if you want to display the “testfile1.txt” content and give a pause of 5 seconds to show the “testfile2.txt” content, execute the below command:

Note: The cat command is used to output the file’s content.

$ cat testfile1.txt && sleep 5s && cat testfile2.txt 
$ cat testfile1.txt ; sleep 5s ; cat testfile2.txt

Similarly, to change the directory to “Desktop” and delay for five seconds before listing out the file in that directory, run the below command:

$ cd ~/Desktop ; sleep 5s ;  ls

Example 3: How to Start any Process After a Specific Time?

Similarly, any other Linux process can also be executed with a specific delay. For instance, to start a video named “movie.mp4” after a two hours delay time, use the below command:

$ sleep 2h && movie.mp4 $ sleep 2h ; movie.mp4

Example 4: How to Verify the Delay Time?

The sleep time adds a pause to the execution. If you to verify the delay time at the time of execution, you can use the time command for that. To pause the system for two seconds and print out the “Time Finished”, use the below command:

$ time(sleep 2s; echo "Time Finished")

The output shows the real-time as 0 minutes and 2 seconds, which verifies the delay of 2 seconds.

Example 5: How to Delay Bash Script?

The sleep command is useful to delay the bash scripts. The long bash scripts take time to process the executions while the next command starts executing. For executing a simple bash script, with a delay of five minutes, the code is given below:

#!/bin/bash
echo "Start Time $(date)"
sleep 5s
echo "Print Time $(date)"

The scripts start with the “#!” which indicates the start of execution and /bin/bash is a directory for bash scripts.

The echo displays the text “Start Time” with a date variable which will print the date & time, and after the 5-second delay, the text “Print Time” with the current date and time will be displayed.

Note: If you try to run the bash script code without enabling the Execute (x) permission, you will get the below error:

Permission denied!

Use the below chmod command to enable the execute (x) permission for that bash script:

$ chmod +x testfile.sh

To run the bash script file named “testfile.sh”, use the following command:

$ ./testfile.sh

The output shows the text with the date & time before and after the delay of 5 seconds.

Bonus Tip: How to Exit From Sleep Mode?

If you have used a large delay time and want to exit the sleep mode, press the shortcut keys “Ctrl + C” to exit the sleep mode.

For instance, to exit the sleep cycle of five minutes, as used in the below command, use the “Ctrl + C” keys.

$ sleep 5m ; cat testfile1.txt

That’s all from this guide!

Conclusion

Linux sleep command is used to delay the executions in the system commands/processes by adding the delay in seconds, minutes, hours, or days; the syntax “sleep number[suffix]” is used. The execution delay can be for single as well as multiple commands. This post has briefly demonstrated the working of the sleep command in Linux with examples.