How to Pause a Bash Script With the Linux Sleep Command

Share
  • April 11, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

The sleep command makes your Linux computer do nothing. Counter-intuitive perhaps, but a period of inactivity is sometimes just what’s needed. This article shows you how to use this Bash shell command effectively.

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

sleep 5

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.

No visible output from sleep 5 command

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:

sleep 1d 4h 7m 5s

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:

sleep 5m 20

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.

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

Using Sleep to Pause Before a Command

Read the remaining 15 paragraphs

Source : How to Pause a Bash Script With the Linux Sleep Command