Stop cron jobs from overlapping and killing your load average

Cron & Task Scheduling (Cron/Systemd Timers/At)

Stop cron jobs from overlapping and killing your load average

🧩 The Challenge

You ever have a script that takes five minutes to run, but you scheduled it to go off every minute because you didn’t check how long it actually takes? Pretty soon you have fifty instances eating all your RAM and the server is crawling.

💡 The Fix

Use flock to lock a file on disk so that only one instance of your script can run at any given time. It is a lifesaver for those heavy database exports that sometimes hang longer than they should.

/usr/bin/flock -n /var/run/my_script.lock /usr/local/bin/my_heavy_script.sh

⚙️ Why It Works

Adding the -n flag makes the command exit immediately if the lock file is held by another process, preventing the stack-up entirely.

🚀 Pro-Tip: Put your lock file in /run/lock to keep things clean and compliant with current filesystem hierarchies.

Linux Tips & Tricks | © ngelinux.com | 11/25/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted