Does Sleep Mode Continue a Local Script

Shell Script Sleep

Introduction to Shell Script Sleep

Just a small analogy before we set our flag to learn the feature, for us humans or any other living creature sleep is an important attribute in the efficient functioning of the body. Similarly, in the software world, not only in shell scripting, but even in cases of other programming languages we do have the feature of "sleep". Just imagine a situation where you have no sleep and continuously fed with some activity which you love doing. The continuous inflow without rest would not only bother you but anyone by your side. Now, let's apply the same scenario in a place where there is a continuous output of information, without giving you time to read the earlier message. Won't it be overwhelming for you.

This is where we would fully understand the true sense of sleep command. The main moto of the command lies in giving time for the user to consume the output of the earlier command. Now coming to a formal definition of sleep command. This command is a utility in the command line which disables the execution of the next command for a specified amount of time. This specified amount of time is given as an argument to the command.

Syntax of Shell Script Sleep

Now let us look at the syntax of sleep command. It is as follows:

sleep <amount>[Suffix]

Here, the sleep is the keyword which denotes sleep action to be executed, <amount> is the time for which the sleep command will be activated, and the suffix is the extension required to identify the quantification of time, i.e. either seconds, minutes, hours or days. The different suffixes are as follows:

  • s: denotes seconds
  • m: denotes minutes
  • h: denotes hours
  • d: denotes days

By default, if nothing is specified seconds is eventually taken as the unit of delay. It is not unusual to see 2 arguments being passed to the command. In those cases, the sum of both the arguments are taken to the total pausing time. For example, if the argument says, 2m 30s then the pause will be for 150 sec. Now, if someone looks at just the sleep command, the first thing which will come to one's mind is that it is not of much use at all, but when mixed with the script it can do wonders in the way one can't even think of.

Some of these ways where sleep can be wonderful are:

  • In cases of network reconnection, we would need to wait for some specified time before reconnecting again otherwise the connection can be blocked thinking the connection is not legit.
  • Pausing before executing a command again, if it has failed once before.
  • In cases of a script copying files from one place where the file is been downloaded from a server, we shouldn't start the copy process as soon as it starts downloading. We would need to wait for a specified time before starting the copy process, otherwise, the copy of the file will be corrupted.
  • A cron job, which has been designed to run after specified time might need sleep command to accomplish the job. Again, a best place for using the sleep command.

There are only a couple of switches available for sleep command. One of them is –help which points to the help file of sleep command. The other one is –version which gives the attributes like, version number, details of copyright, authors and the licenses.

Examples of Shell Script Sleep

In the example we would look at the code, the usage of syntax and then the output. We will make sure that at the end of this article you would have a clear understanding of when and where to apply sleep in shell script! So, let us start with a bunch of examples to get a better feel of the command.

Example #1

Running the sleep command without a suffix.

Code:

echo "************************************************"
echo "Hello Fellas!"
echo "Demo for Case: No Suffix"
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
#!/bin/bash
x=$(( $1 * 10 ))
while [ $x -gt 0 ] do
sleep $1
echo "No suffix present! Wait for $1 seconds"
x=$(( $x - $1 ))
done
echo "Done!!"

Output:

Shell Script Sleep-1.1

In the above code, we would wait for the exact amount of time specified in the argument before printing the line "Waiting for…". This is very analogous to the scenario when you would have to wait for x seconds, before re-executing the same command again and this re-execution will be done 10 times before giving up on re-connection. The output is given below.

Example #2

Running the script with a suffix.

Code:

echo "************************************************"
echo "Hello Fellas!"
echo " This script is to understand how to run the script with a suffix "
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
#!/bin/bash
echo "Waiting for  $1 minutes..."
sleep $1
echo "Task Completed"

Output:

Shell Script Sleep-1.2

When a suffix is added, the command of "Task completed" will wait for the specified time in the argument and then echo out the required comment. In the above code, the code will be paused for 3 seconds. The output is as follows:

Conclusion

In a nutshell, sleep command is one of the most powerful commands in bash script which when executed alone would mean meaningless, but in cases of re-trying or scheduling a job after some interval of time can bring wonders in the world of automation. You are encouraged to explore different ways you can use the concepts learnt here in real world projects you would be handling day to day. In this article, we brought out all facets of sleep command and these scripts above are a starter pack for you to use and start building your magical script doing wonders!

Recommended Articles

This is a guide to Shell Script Sleep. Here we also discuss the Introduction and syntax of shell script sleep along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Shell Script Set Variable
  2. Shell Script Operators
  3. If Else in Shell Scripting
  4. Shell Script Read File

mooregonell63.blogspot.com

Source: https://www.educba.com/shell-script-sleep/

0 Response to "Does Sleep Mode Continue a Local Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel