Tuesday, January 9, 2018

Random sleep time in Bash

Recently, I needed to setup a script that would sleep a certain number of seconds between 1 and 8.  Stumbled across this particular item, which was adapted to my need:


$ sleep $[ ( $RANDOM % 8 ) + 1 ]s

This would sleep anywhere from 1 second to 8 seconds before continuing.  If the need comes up to set to a different limits, you can change the 8 to the max number and the 1 to the minimum.  Note that increasing the minimum value will affect the max number as well if min > 1.

Sourced from: http://blog.buberel.org/2010/07/howto-random-sleep-duration-in-bash.html