How to make a script sleep for a random amount of seconds

Reading Time: < 1 minute

If you want to insert a sleep in a script for a random number of seconds, you can do the following:

        NUMBER=$[ ( $RANDOM % 60 )  + 1 ]
        sleep $NUMBER

Replace 60 with the interval chosen and you will get a random sleep between 1 and 60 seconds.

This can be useful if you are trying to fetch data from a server who’s monitoring the number of connections you are opening and tends to blacklist you if you do too much.

Reading Time: < 1 minute

If you want to insert a sleep in a script for a random number of seconds, you can do the following:

        NUMBER=$[ ( $RANDOM % 60 )  + 1 ]
        sleep $NUMBER

Replace 60 with the interval chosen and you will get a random sleep between 1 and 60 seconds.

This can be useful if you are trying to fetch data from a server who’s monitoring the number of connections you are opening and tends to blacklist you if you do too much.

By Marc Olivier Meunier

Marc has spent the past few years putting oil on the fire of growth companies. He leads by example and puts a lot of emphasis on inclusion, constantly working to create a safe environment. A warm leader with a passion for memorable experiences and innovation.
Marc is available for contracting as a fractional Head of Customer Experience.
Find Marc on Linkedin

Leave a Reply

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