Stop crontab from running your jobs twice because of clock drift

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

Stop crontab from running your jobs twice because of clock drift

🧩 The Challenge

Dealing with a server that jumps back in time because of an NTP glitch is bad enough, but waking up to duplicate database entries because cron fired your backup job twice is a nightmare. I’ve spent way too long cleaning up double-inserted records because the system clock decided to take a stroll into the past.

💡 The Fix

Use flock to place a lock file on your task so that if a previous instance is still running, the new one just exits gracefully instead of stomping on the data. It’s the only way to sleep easy when your task timing gets weird.

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

⚙️ Why It Works

The -n flag tells flock to immediately fail if it can’t get the lock, which keeps your process queue from growing into a massive stack of redundant jobs. If the lock is held, the command just exits and saves you from a total mess.

🚀 Pro-Tip: Put the lock file in /run/lock instead of /tmp if you want to be pedantic about standard filesystem hierarchy paths.

Linux Tips & Tricks | © ngelinux.com | 9/29/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted