Cron & Task Scheduling (Cron/Systemd Timers/At)
Stop cron jobs from nuking your server’s load average
🧩 The Challenge
You schedule five heavy database backups for midnight, and suddenly the whole machine locks up because they all hit the disk at the exact same second. I spent half a morning once figuring out why my load average looked like a mountain range every single night.
💡 The Fix
Use the flock command to wrap your cron scripts so they check if an instance is already running before starting. It keeps your system from thrashing itself to death when things take longer than expected.
* * * * * flock -n /tmp/my-job.lock /usr/local/bin/backup-script.sh
⚙️ Why It Works
Setting the -n flag tells flock to immediately exit if the lock file is already held, preventing a pile-up of identical processes. By pointing it at a specific lock file in /tmp or /var/run, you create an exclusive gatekeeper for your script.
🚀 Pro-Tip: Use a descriptive name for your lock file so you aren’t hunting through /tmp wondering which script owns which process.
Linux Tips & Tricks | © ngelinux.com | 10/8/2026
