You must run a job only on the primary node of a two-node cluster, but both nodes have the same cron. How?
AdvancedScenarioScheduling, automation and scripting
Answer
Situation read → Make the job check leadership before acting.
What I'd do →
- If there's a floating VIP (keepalived/Pacemaker), check it:
ip -o addr show | grep -q '10.0.0.100/' || exit 0
- Or take a distributed lock (Consul
consul lock, etcd lease, a DB advisory lockSELECT pg_try_advisory_lock(42)). - Better: make it a cluster resource in Pacemaker, or a Kubernetes CronJob with
concurrencyPolicy: Forbid.
Why → Duplicated runs cause double emails, double billing and similar issues.
Interview tip → Mention making jobs idempotent so a double run is harmless anyway.
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?BeginnerYou have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?IntermediateA 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?Beginner