Site icon New Generation Enterprise Linux

Catch cron jobs that run long and choke your system

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

Catch cron jobs that run long and choke your system

đź§© The Challenge

Everyone thinks cron is a set-and-forget deal until a backup script hangs and suddenly you have fifty instances of it fighting for disk IO. It turns into a total dumpster fire before you even wake up to the alerts.

đź’ˇ The Fix

Use the flock utility to slap a file lock on your script so that no matter how long the first instance takes, the second one refuses to start. It keeps your server from spiraling into a performance death loop.

* * * * * /usr/bin/flock -n /tmp/my_script.lock /usr/local/bin/my_script.sh

⚙️ Why It Works

Adding the -n flag makes the command exit immediately if it can’t grab the lock instead of waiting around in a queue. It’s the simplest way to ensure only one instance of your logic is hitting the CPU at any given time.

🚀 Pro-Tip: Use a dedicated directory under /run/lock for these instead of /tmp so a random reboot or janitor script doesn’t wipe your lock files.

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

0 0 votes
Article Rating
Exit mobile version