Randomizing cron scheduling across multiple servers
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.
Thanks for your website and tips. IMHO, this is a risky change — anyone testing this will wipe their existing crontab. The overall idea is great and something that I do across oracle jobs, windows scheduled tasks and crontab environments already.
Perhaps if you add another command before root example and use for all cases
crontab -l > $CURRENT_CRONTAB
So, here’s my take to create an “Append” if crontab already exists
Yes, appending the new commands to en existing crontab is obviously very much preferred instead of generating a new one :) Mine was more of a POC to show how to generate cron execution times in certain scenarios, for example when multiple machines are provisioned and there is a need to “unevenly” distribute outgoing connections triggered via cron from multiple nodes to a single remote server etc. Thanks for commenting!