Friday, April 1, 2016

random sleep duration in bash shell

Here is a quickie shell script (bash) tip. If you ever want to insert a random sleep/pause into a running script:

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

This will introduce a 1-10 second random sleep/pause in the script. Can be handy at times.

I put a cron job to call this script every 3 minutes:

echo '*/3 * * * * root /usr/local/bin/heartbeat.sh >>/var/log/heartbeat.log 2>/var/log/heartbeat.err' >/etc/cron.d/heartbeat



#!/bin/bash

#### To Update AAW node session
#### by Lambert
#### Version 1.0
#### Release Date: 2016-03-29

sleep $((RANDOM % 30))
wget -O /var/log/heartbeat.wget.log $SUBMIT_URL

2 comments:

  1. That is a random sleep from 1 to 30 seconds!

    ReplyDelete