- Linux sleep Command | Explained With Examples
- What is Linux sleep Command?
- How to Use sleep Command in Linux?
- Example 1: How to Delay for Specific Time?
- Delay for Second
- Delay for Minutes
- Delay for Hours
- Delay for Days
- Delay for Specific Hours and Minutes
- Example 2: How to Delay the Multiple Linux Commands?
- Example 3: How to Start any Process After a Specific Time?
- Example 4: How to Verify the Delay Time?
- Example 5: How to Delay Bash Script?
- Permission denied!
- Bonus Tip: How to Exit From Sleep Mode?
- Conclusion
- Sleep Command in Linux
- Sleep command syntax:
- Example-1: sleep command without any suffix
- Example-2: sleep command with a minute suffix
- Example-3: sleep command with hour suffix
- Example-4: sleep command with loop
- Example-5: sleep command in the terminal with other commands
- Example-6: Using sleep command from the command prompt
- About the author
- Fahmida Yesmin
- Using Linux Sleep Command in Bash Scripts
- Bash sleep command Examples
- Sleep command without suffix counts in seconds
- Sleep command with minute or hour or day suffix
- Sleep command with a combination of second, minute, hour and day
- Bonus Tip: Sleep less than a second
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:
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
We can use the sleep command differently. Check the syntax for the sleep command below:
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:
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:
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:
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:
Delay for Hours
For sleeping the system for three hours (h), utilize the below command:
If you want to run the command “cd ~/Downloads”, after five hours, run the below command in the terminal:
Delay for Days
To delay the execution of the system for three days, use the following command:
If you want to play an audio testfile.mp3 after/schedule three days, execute the following command:
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:
For example, to have a pause of 2 hours and 2 minutes to play a video named testfile.mp4, execute the following command:
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:
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:
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:
To run the bash script file named “testfile.sh”, use the following command:
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.
Sleep Command in Linux
Sleep command is used to delay for a fixed amount of time during the execution of any script. When the coder needs to pause the execution of any command for the particular purpose then this command is used with the particular time value. You can set the delay amount by seconds (s), minutes (m), hours (h) and days (d). This tutorial will help you to learn the use of sleep command by using different bash scripts.
Sleep command syntax:
sleep number[suffix]
You can use any integer or fractional number as time value. Suffix part is optional for this command. If you omit suffix then time value is calculated as seconds by default. You can use s, m, h and d as suffix value. The following examples show the use of sleep command with different suffixes.
Example-1: sleep command without any suffix
In the following script, sleep command is used with numeric value 2 only and no suffix is used. So, if you run the script then the string “Task completed” will print after waiting for 2 seconds.
echo «Waiting for 2 seconds. »
sleep 2
echo «Task Completed»
Run the bash file with time command to show the three types of time values to run the script. The output shows the time used by a system, user and real time.
Example-2: sleep command with a minute suffix
In the following script, ‘m‘ is used as the suffix with sleep command. Here, the time value is 0.05 minutes. After waiting 0.05 minutes, “Task completed” message will be printed.
echo «Waiting for 0.05 minutes. »
sleep 0.05m
echo «Task Completed»
Run the script with time command like the first example.
Example-3: sleep command with hour suffix
In the following script, ‘h‘ is used as the suffix with sleep command. Here, the time value is 0.003 hour. After waiting 0.003 hour “Task completed” should be printed on the screen but it requires more times in reality when ‘h’ suffix is used.
echo «Waiting for 0.003 hours. »
sleep 0.003h
echo «Task Completed»
Example-4: sleep command with loop
You can use sleep command for various purposes. In the following example, sleep command is used with while loop. Initially, the value of the variable n is set to 1 and the value of n will be incremented by 1 for 4 times in every 2 seconds interval. So, when will you run the script, each output will appear after waiting 2 seconds.
#!/bin/bash
n = 1
while [ $n -lt 5 ]
do
echo «The value of n is now $n »
sleep 2s
echo » »
( ( n = $n + 1 ) )
done
Example-5: sleep command in the terminal with other commands
Suppose, you want to run multiple commands and set the fixed time interval between the outputs of two commands, then you can use sleep command to do that task. In this example, the command ls and pwd are with sleep command. After executing the command, ls command will show the directory list of the current directory and show the current working directory path after waiting for 2 seconds.
Example-6: Using sleep command from the command prompt
sleep command is used between two echo commands in the following example. Three time values will be displayed after executing the command.
sleep command is a useful command when you need to write a bash script with multiple commands or tasks, the output of any command may require a large amount of time and other command need to wait for completing the task of the previous command. For example, you want to download sequential files and next download can’t be started before completing the previous download. In this case, it is better to sleep command before each download to wait for the fixed amount of time.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
Using Linux Sleep Command in Bash Scripts
This tutorial shows you how to use sleep commands and its various options in bash scripts.
Linux sleep command is one of the simplest commands out there. As you can guess from the name, its only function is to sleep. In other words, it introduces a delay for a specified time.
So, if you use the sleep command with x and the next command can only be run after x seconds.
Sleep command has a simple syntax:
In here, the suffix could be:
- s for seconds. This is the default.
- m for minutes.
- h for hours.
- d for days.
Let’s see some examples of the sleep command.
Bash sleep command Examples
Though you can use it in a shell directly, the sleep command is commonly used to introduce a delay in the execution of a bash script. I am going to show the usage of sleep command through sample bash scripts.
Sleep command without suffix counts in seconds
Suppose you want pause your bash script for 5 seconds, you can use sleep like this:
In a sample bash script, it could look like this:
!/bin/bash echo "Sleeping for 5 seconds…" sleep 5 echo "Completed"
If you run it with the time command, you’ll see that the bash script actually ran for (a slightly) more than 5 seconds.
time ./sleep.sh Sleeping for 5 seconds… Completed real 0m5.008s user 0m0.000s sys 0m0.007s
Sleep command with minute or hour or day suffix
You can specify the sleep time in minutes in the following way:
This will pause the script/shell for one minute. If you want to delay the script in hours, you can do that with the h option:
Even if you want to pause the bash script for days, you can do that with the d suffix:
This could help if you want to run on alternate days or week days.
Sleep command with a combination of second, minute, hour and day
You are not obliged to use only one suffix at a time. You can use more than one suffix and the duration of the sleep is the sum of all the suffix.
For example, if you use the follow command:
This will keep the script waiting for 1 hour, 10 minutes and 5 seconds. Note that the s suffix is still optional here.
Bonus Tip: Sleep less than a second
You might have noticed that the smallest unit of time in the sleep command is second. But what if your bash script to sleep for milliseconds?
The good thing is that you can use floating point (decimal points) with sleep command.
So if you want to introduce a 5 milliseconds pause, use it like this:
You can also use decimal points with other suffixes.
It will introduce a delay of 1 hour, 37 minutes and 30 seconds.
I hope you didn’t sleep while reading these examples of sleep command 😉
If you are interested in shell scripting, perhaps you would like reading about string comparison in bash as well. If you have questions or suggestions, please feel free to ask.