You have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?
IntermediateScenarioScheduling, automation and scripting
Answer
Situation read → Thundering herd; spread the start times.
What I'd do →
- With systemd timers:
RandomizedDelaySec=1800(andFixedRandomDelay=truefor a stable per-host offset). - With cron, add a random sleep:
0 0 * * * sleep $((RANDOM % 1800)); /usr/local/bin/job.sh(needsSHELL=/bin/bash). - Or derive the minute from the hostname in your config management (Ansible
randomfilter withseed=inventory_hostname).
Why → Jitter turns a spike into a flat load.
Interview tip → Mention that $RANDOM is a bash feature and cron's default shell is /bin/sh.
More on Scheduling, automation and scripting
A database dump must run every night at 1:30 AM. How do you schedule it with cron so it is reliable?BeginnerConvert that nightly cron job to a systemd timer. Why would you?IntermediateA cron job runs every 5 minutes, but sometimes takes 8 minutes and two copies overlap, corrupting data. How do you prevent that?BeginnerA job must run on the last day of every month. Cron has no "last day" field. What do you do?IntermediateYou inherited a server and need to know every scheduled job on it. Where do you look?BeginnerA one-off data fix must run at 9 PM tonight, just once. How do you schedule it?Beginner