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

The cursor will disappear for five seconds and then return. What happened? Using sleep on the command line instructs Bash to suspend processing for the duration you provided. In our example this was five seconds.

We can pass durations to sleep 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 cause sleep to pause for one day, four hours, seven minutes and five seconds, use a command like this:

The s suffix (for seconds) is optional. With no suffix, sleep will treat any duration as seconds. Suppose you wanted to have sleep pause for five minutes and twenty seconds. One correct format of this command is:

If you forget to provide the m suffix on the minutes duration, you will instruct sleep to pause for five seconds and then again for twenty seconds. So sleep will pause for 25 seconds.

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

All of the following (increasingly eccentric) commands tell sleep to pause for 10 seconds.

Using Sleep to Pause Before a Command

The sleep command can be used to give a pause before the execution of a command. This command would pause for 15  seconds and then give a bleep.

Using Sleep to Pause Between Two Commands

You can use sleep to give a 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:

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 amount of time. Typically, you’d do this to allow some process sufficient time to complete before the script continues its processing. You can also use it to rate-limit the requests a script makes to another resource.

To demonstrate exactly that, here is a script that calls out to a Google web service using curl. When you query the web service with the ISBN number of a book, it responds with a dump of JSON data regarding that book. We can parse that data by passing it through the jq utility to retrieve the title of the book. So that the script doesn’t stress the web service, it sleeps for one second between web requests.

Create a file containing the following text, and save it as check_book.sh.

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

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

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

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

The requests are made to the Google web service at one second intervals. The title of the book will appear shortly after each ISBN number is queried.

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 purely to illustrate a valid use of the sleep command. If you wish to read more about the two main components of the script, refer to the  curl project page and the  jq on-line manual.