Updated March 29, 2023
Introduction to Bash Sleep Command
Sleep as a command is immensely used in an application where we would like to pause the code execution for some time interval. Now the question would arise on why would we need to pause the code? The answer to that is very simple; in some scenarios, there might be a requirement of communication between hardware, and that leads to a loss in time, and the results from these interactions will be further used in the flow of the code. Then it becomes imperative to pause the code execution so as to not encounter any errors. It is like a dummy execution that runs through the time specified. In this topic, we are going to learn about Bash Sleep Command.
Syntax
At first, we will have a first glance at what the syntax is:
sleep <number appropriate>{Suffix}
Now let us break these down into different elements on which this syntax is built on. Here, sleep is the keyword that invokes the command to execute the utility of pausing the code intermittently. Next, the <number appropriate> is nothing but the time for the code to pause. We need to be careful that this is just a number and doesn’t include any units to it. Now we would wonder how the code knows if it has to pause for that number of seconds or hours or days or months or years! That is where the {Suffix} plays the role; {Suffix} is nothing but the units which follow the number. Using this command would know the period for which the code needs to pause. The suffix for this bash command is used as per the utility of the code requirements, and the list of different suffix options which are present are:
S: This is for unit seconds.
M: This is for unit minutes.
H: This is for unit hours.
D: This is for unit days.
Anything more than days needs to be calculated in terms of days only. Earlier implementations of Linux required the <number appropriate> to be an integer, but nowadays, these can even be floating-point numbers. Also, in case there is multiple numbers, which is very unrealistic, Linux makes sure to sum them up and use a single number! Now, {Suffix} is an optional feature that is added to the sleep command. In the case where nothing is specified in units, by default, it will assume the {Suffix} to be seconds.
Another syntax which is used is:
sleep OPTION
Here again, sleep is an external command which invokes the utility of pausing the code, and OPTION is to help developers with working of sleep command. In the further course of time, in another section, we would look at all the options which are available under this hood!
How does Sleep Command work in Bash?
Now that we know the syntax of the sleep command, it becomes even important to understand the working principle behind the command. So, the next question we would like to answer is what does sleep command does in bash, which will ultimately help us answer how sleep command works.
Sleep commands help in suspending the code execution for the specified amount of time. This code execution is nothing but the net command execution in the pipeline. Henceforth creating a dummy program that runs for the specified time without any intention apart from killing time! It can be set as an analogous system to our alarm clock, which rings after a specified amount of time to notify. Assume this as the alarm clock for a power nap at work for 15 minutes!
Essentially this set helps in regulating a repetitive task through a scheduler that has this utility of sleep command. For example, let us say one would need to back up files into another system daily. Now, one can write a code to back up those files wrapped within a sleep command, which will pause the code for a day! Simple! In case you have heard about the cron job, the sleep utility is exactly the utility it exploits!
Sleep Command Options
As we have mentioned in the syntax about the different sleep command options, let us look at them one by one:
1. sleep <number appropriate>{Suffix}
Code:
echo "Explaining the utility of sleep command"
echo "Time right now is:"
date=echo date
echo "Going to sleep for 5 seconds"
sleep 5s
echo "Time after getting up from sleep is:"
date=echo date
Output:
We can see that before the sleep command is executed, the time was 6:56:50, and after 5 seconds of sleep, the time is 6:56:55, which essentially signifies that the code was on pause for 5 seconds! For the convenience of utility, we are not showing the utility of the other options like minutes, hours but would highly encourage to use these utilities at the bay of the learner and understand that those are no different from the second’s option and can be used in cases as required for fulfilling utilities.
2. sleep –help
Code:
echo "Explaining the utility of sleep command of --help command"
sleep --help
Output:
This option eventually portrays the documentation or the help file of the sleep command utility. Once the command is executed, it will exit the bash script.
3. sleep –version
Code:
echo "Explaining the utility of sleep command of --version command"
sleep --version
Output:
This option eventually portrays the version containing the details like version number, copyright details, license, and authors. Thus, from a usage standpoint, one can use the commands without any restrictions of the law as they can be used even commercially as per the license terms and conditions.
Conclusion
This module has looked into all aspects of sleep attributes that are widely used in the industry. By now, we can easily set an analogous scenario of sleep utility as a dummy job that is created in delaying an execution. However, with the above syntax and examples, we would highly recommend you try them on your own to have the confidence to use them in real-world examples.
Recommended Articles
This is a guide to Bash Sleep Command. Here we discuss How does Sleep Command works in Bash and Options, along with codes and outputs. You may also have a look at the following articles to learn more –