Randomizing cron scheduling across multiple servers

Author: | Posted in howto No comments

This is not about running cron at random hours every time on each server, but configuring different time schedules for multiple machines. This is especially useful when dealing with many servers as having the same cron entries on all of them might affect LAN / WAN bandwidth or increase load on the master server (yum internal repository, backup server) that all of the machines are trying to reach at the same time.

My approach on this is to write a small script to generate random hours / minutes /etc. and have that script run on each server. This can be done during kickstart installations (%post section) or at any time after the OS is installed (e.g. by installing an RPM package that includes crontab configuration or simply by pushing and triggering the script execution automatically).

Here is a sample shell coding which makes use of the shuf utility:

 

Generate random hours between 9AM-6PM:
MINS=`/usr/bin/shuf -i 1-59 -n 1`
HRS=`/usr/bin/shuf -i 9-17 -n 1`

 

Install crontab:

 

  • for root:
echo "$MINS $HRS * * *    $COMMAND" > /tmp/cronjob
cat /tmp/cronjob | crontab

 

  • for generic user:
echo "$MINS $HRS * * *    $COMMAND" | crontab -u $USER -

Hope this helps.

Add Your Comment