Running Bash Script at random time

Viewed 1042

I have as iperf.sh shell script in multiple sub servers that runs at every " 1,14,28,42,50 * * * * " and pings the iperf server to check bandwidth , is there any way to randomize this cron or setting up a shell script that sleeps and runs at random time...?

[ Note : The issue that i am facing with this classic cron system is all sub-servers are running the iperf.sh script at the same time and my main-Iperf server is getting high cpu utilization which is resulting to improper ping data. ]

Thanks In Advance.

1 Answers

You can add a randomized wait period at the start of your script (or even in the crontab itself, as suggested in the comments).

I recommend GNU shuf which will be more portable than $RANDOM (since not all shells will support it, e.g. dash won't).

sleep $(shuf -i5-20 -n1)

# Rest of script

You can experiment with the range of random wait periods (5 to 20 seconds in this example).

Related