ofIn a big Unix environment you likely install cron jobs like this on a thousand systems:
39 15 * * * { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1
If all the system clocks are synchronized (usually via NTP), these jobs run *exactly* at the same time.
If the cron job accesses a shared resource (network file system, database, send mail, ...) there is a load peak!
The solution is to add individual delays.
Say the delay should be within 60 seconds.
bash and ksh have $RANDOM and arithmetics; the following looks promising
39 15 * * * sleep $((RANDOM % 60)) && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1
Unfortunately %
has a special meaning to "cron". It must be escaped \%
, and the shell then sees \%
.
This is really an obstacle!
I have found the following solutions:
39 15 * * * perl -e 'sleep rand(60)' && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1
39 15 * * * sleep `cksum /etc/hosts | awk '{print $1'\%'60}'` && { /usr/local/monitoring/sendstats ; } >/dev/null 2>&1
The latter has a static delay - but still individual.
Do you have other solutions?