Here is an article that explain a way to get consistent "random" number. The problem appears in virtual environments, like
lxc, where
hostid is the same. The sample below solves this problem by generating "random" number from a network adapter (eth0)
MAC address. So here it is (file
func/random_sleep)
#!/bin/bash
#
# random_sleep shift_max rnd_max
#
# shift_max - a number to generate consistent "random"
# number.
# rnd_max - a random number to add
random_sleep()
{
shift_max=600
if [ ! -z $1 ]; then shift_max=$1; fi
rnd_max=60
if [ ! -z $2 ]; then rnd_max=$2; fi
# shift is a number betweem 0 and 600
shift=`/sbin/ifconfig eth0 | /bin/grep HWaddr | \
/bin/sed -e 's/.*HWaddr //' | /usr/bin/tr -d ':'`
shift=$((0x$shift%$shift_max))
# Sleep a random amount of time + shift
sleep_time=$(($RANDOM % $rnd_max + $shift))
echo "Sleeping for $sleep_time seconds..."
/bin/sleep $sleep_time
}
Here is how you can use it:
#!/bin/bash
. /usr/local/sbin/func/random_sleep
# or
# . `dirname $_`/func/random_sleep
random_sleep 300 60
No comments :
Post a Comment