Pause a Bash Script,The sleep command makes your Linux pc do not anything. Counter-intuitive perhaps, but a period of inaction is once in a while simply what’s wanted. This article indicates you the way to use this Bash shell command successfully.
Using sleep is straightforward. On the command line type sleep, a area, a number, after which press Enter.sleep 5
The cursor will disappear for five seconds after which go back. What occurred? Using sleep at the command line instructs Bash to droop processing for the duration you provided. In our instance this changed into 5 seconds.
We can bypass periods to sleep in days, hours, and minutes, as well as in seconds. To try this include a suffix of either d, h, m, or s with the period. To motive sleep to pause for someday, four hours, seven mins and five seconds, use a command like this:sleep 1d 4h 7m 5s
Linux Sleep Command
Pause a Bash Script,The s suffix (for seconds) is non-obligatory. With no suffix, sleep will treat any period as seconds. Suppose you desired to have sleep pause for 5 mins and twenty seconds. One correct format of this command is:sleep 5m 20
If you forget to provide the m suffix at the mins length, you may instruct sleep to pause for 5 seconds and however for twenty seconds. So sleep will pause for 25 seconds.
Many instructions require you to offer parameters in a specific order, but sleep is very forgiving. You can offer them in any order and sleep will make experience out of them. You also can provide a floating factor number as a parameter. For instance, 0.5h is a legitimate manner to indicate you want sleep to pause for half of an hour.
All of the following (increasingly eccentric) instructions tell sleep to pause for 10 seconds.sleep 10 sleep 5 5s Sleep 1 1 1s 1 1 1s 1 2 sleep 0.16667m
Using Sleep to Pause Before a Command
The sleep command may be used to offer a pause before the execution of a command. This command could pause for 15 seconds after which give a bleep.sleep 15 && echo -en ‘\007’
Using Sleep to Pause Between Two Commands
You can use sleep to present a pause between instructions. This command could listing the files to your Documents listing, pause for five seconds and then exchange the modern-day operating directory to your private home listing:ls -R ~/Documents && sleep 5 && cd ~
Using Sleep to Pause Execution of a Script
You can use the sleep command in shell scripts to pause execution of the script for a precise quantity of time. Typically, you’d do this to permit a few manner sufficient time to complete before the script keeps its processing. You also can use it to fee-restrict the requests a script makes to another useful resource.
To demonstrate exactly that, here is a script that calls out to a Google internet provider using curl. When you query the web provider with the ISBN variety of a ebook, it responds with a dump of JSON statistics concerning that e book. We can parse that statistics by means of passing it through the jq utility to retrieve the name of the e book. So that the script doesn’t stress the web service, it sleeps for one 2nd between web requests.
Pause a Bash Script,Create a report containing the following text, and keep it as check_book.Sh.
for book in `cat $1`
curl -s https://www.googleapis.com/books/v1/volumes?q=isbn:$book | jq '.items | . | .volumeInfo.title'
echo "All done."
Type the subsequent command to set the execution permissions and make the script executable.chmod +x check_book.sh
The script calls for the curl and jq utilities. Use apt-get to put in these programs onto your gadget in case you’re the use of Ubuntu or some other Debian-primarily based distribution. On different Linux distributions, use your Linux distribution’s package deal management tool as an alternative.sudo apt-get install curl sudo apt-get install jq
Execution of a Script
Create a textual content record containing the subsequent numbers, and keep it as books.Txt.9781565921276 9781874416685 9781565921672 9780521431088 9781491941591
Run the check_book.Sh script and pass inside the books.Txt report as a parameter../check_book.sh books.txt
The requests are made to the Google net carrier at one 2d intervals. The name of the e-book will seem quickly after each ISBN wide variety is queried.
That’s all there’s to sleep. The inner workings of the check_book.Sh script are beyond the scope of this article. The script became selected purely to illustrate a valid use of the sleep command. If you wish to examine extra approximately the two primary additives of the script, refer to the curl assignment page and the jq on-line manual.