Cron & Task Scheduling (Cron/Systemd Timers/At)
Stop letting your cron jobs overlap and kill your system
đź§© The Challenge
You’ve got that one backup script that runs every hour, but sometimes the data volume spikes and it’s still grinding away when the next instance starts. Next thing you know, you’ve got ten processes fighting over the disk and the load average is hitting the ceiling.
đź’ˇ The Fix
Use flock to force an exclusive lock on the process so that if a second instance kicks off, it just exits immediately instead of stacking up. It’s the easiest way to prevent a self-inflicted DDoS.
* * * * * /usr/bin/flock -n /tmp/my_backup.lock /usr/local/bin/backup_script.sh
⚙️ Why It Works
Setting the -n flag tells flock to fail non-zero immediately if it can’t get the lock, which saves your server from the pile-up. Without this, your crontab is just a ticking time bomb.
🚀 Pro-Tip: If you need to know why it failed, redirect the stderr of the cron job to a log file so you can catch the lock contention warnings later.
Linux Tips & Tricks | © ngelinux.com | 10/20/2026
