How to Pause a Bash Script With the Linux Sleep Command

Bash shell on the Unity desktop conceptFatmawati Achmad Zaenuri / Shutterstock.com

The sleep command causes your Linux computer to do nothing. Counterintuitive perhaps, but a period of inactivity is sometimes just what you need. This article describes how to effectively use this Bash shell command.

Using sleep is easy. On the command line, type sleep, a space, a number, and then press Enter.

to sleep 5

The cursor will disappear for five seconds and then return. What happened? Using standby on the command line tells Bash to suspend processing for the specified duration. In our example it was five seconds.

No visible output of the sleep 5 command

We can spend sleep times in days, hours and minutes, as well as in seconds. To do this, include a suffix of either d, h, m, or s with the duration. To sleep for one day, four hours, seven minutes, and five seconds, use a command like this:

to sleep 1d 4h 7m 5s

The suffix s (for seconds) is optional. In the absence of suffix, sleep will treat any duration in seconds. Suppose you want a five-minute, twenty-second sleep break. A correct format of this command is:

to sleep 5m 20

If you forget to provide the suffix m for the duration of the minutes, you will ask the day before to pause for five seconds, then again for twenty seconds. So, sleep will be paused for 25 seconds.

Many commands require that you provide parameters in a specific order, but sleep is very tolerant. You can provide them in any order and sleep will make sense of them. You can also supply a floating point number as a parameter. For example, 0.5h is a valid way to indicate that you want sleep to take half an hour.

All the following commands (more and more eccentric) indicate the day before to pause for 10 seconds.

to sleep 10
to sleep 5 5s
Sleep 1 1 1s 1 1 1s 1 2
to sleep 0.16667m

Use the day before to pause before a command

The sleep command can be used to pause before a command is executed. This command would stop for 15 seconds and then beep.

sleep 15 && echo -en & # 39; 007 & # 39;

Using the day before to pause between two commands

You can use the sleep mode to pause between two commands. This command would list the files in your Documents directory, pause for five seconds, and then change the current working directory to your home directory:

ls -R ~ / Documents && sleep 5 && cd ~

Release of two commands separated by a sleep

Using the standby to pause the execution of a script

You can use the sleep command in the shell scripts to pause the script for a specified period of time. Typically, you must do this to allow a process to be long enough for the script to continue processing. You can also use it to limit requests for a script to another resource.

To illustrate exactly this, here is a script that calls a Google web service using curl. When you query the web service with the ISBN number of a book, he responds with a JSON data dump concerning this book. We can analyze this data by passing them through the utility jq to retrieve the title of the book. In order for the script to not stress the web service, it stays idle for one second between web requests.

Create a file that contains the following text and save it as check_book.sh.

#! / bin / bash

for book in `cat $ 1`
make
echo $ book ":"
curl -s https://www.googleapis.com/books/v1/volumes?q=isbn:$book | jq & # 39; .items | .[] | .volumeInfo.title & # 39;
echo ""
to sleep 1
completed

echo "Everything is done."

Type the following command to set the permissions to run and make the script executable.

chmod + x check_book.sh

The script requires the utilities curl and jq. Use apt-get to install these packages on your system if you are using Ubuntu or another Debian-based distribution. On other Linux distributions, use the package management tool of your Linux distribution instead.

sudo apt-get install curl
sudo apt-get install jq

Create a text file containing the following numbers and save it as books.txt.

9781565921276
9781874416685
9781565921672
9780521431088
9781491941591

Run the check_book.sh script and pass the books.txt file as a parameter.

./check_book.sh books.txt

Exit shell script check_book.sh

Requests are sent to Google's web service every second. The title of the book will appear shortly after each ISBN.

That's all there is to sleep. The inner workings of the check_book.sh script are beyond the scope of this article. The script was chosen only to illustrate a valid use of the sleep command. If you want to know more about the two main components of the script, refer to the section curl project page and the jq manual online.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.