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 7 years putting oil on the fire of a hyper growth ad tech company. At Smartly.io he was in charge of scaling the support and its culture. He leads by example and puts a lot of emphasis on diversity and inclusion, constantly working to create a safe environment. A warm leader with a passion for memorable experiences and innovation.
Find me on Linkedin

Leave a Reply

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