14
May

Sleeping for a random amount of time in a shell script

Ever want to make a shell script pause…

You can use the special $RANDOM environment variable to get a random number and then divide it by the maximum number of seconds that you want to wait. Use the remainder as the number of seconds to sleep since it will always be between zero and the max you specified. This example will sleep anywhere between zero and 10 minutes (600 seconds)

 /bin/sleep/sleep   `/usr/bin/expr $RANDOM % 600`

Of course – those who really are keen will note that it isn’t truly random. The maximum value for $RANDOM is 32767 which is not evenly divisible by most likely values – but its close enough for me ;-)

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment