Cron jobs overlapping: how to stop two runs colliding

When a cron job takes longer than its interval, runs pile up and corrupt data or exhaust the server. How to prevent overlap with flock, lock files and timeouts, and spot it early.

· 7 min read · By the Spot Downtime team

A job scheduled every five minutes usually finishes in ten seconds. Then one day the database is slow, the job takes seven minutes, and the next run starts while the first is still going. Now two copies process the same rows, or fight over the same file. If the slowness was caused by load, the extra copy makes it worse, and a third arrives five minutes later.

Cron does not care. It starts every run on schedule, whether or not the previous one has finished. Preventing overlap is your job.

What overlap breaks

  • Duplicate work: emails sent twice, invoices created twice, the same queue items handled by two workers.
  • Corrupted output: two processes writing the same file or the same temporary directory.
  • Resource exhaustion: processes stack up until the server runs out of memory or database connections.
  • Deadlocks and lock waits in the database, which slow the rest of the application.

Confirm that it is happening

Look for more than one instance of the job running at once:

ps aux | grep '[b]ackup.sh'
pgrep -af backup.sh

If several lines come back, runs are overlapping. Check how long a normal run takes, for example by logging start and end timestamps, and compare it with the schedule interval. If the typical duration is close to the interval, you are one slow day from trouble.

Fix 1: flock, the simplest guard

On Linux, flock takes a lock on a file and runs your command only if it gets the lock. The -n flag makes it give up immediately instead of waiting, so a new run is skipped while the old one is active.

*/5 * * * * flock -n /var/lock/sync-orders.lock /opt/jobs/sync-orders.sh

The kernel releases the lock when the process exits, even if it crashes or is killed, so you do not end up with a stale lock. That is the main reason to prefer flock over hand-made lock files.

If you would rather queue than skip, use -w to wait up to a number of seconds:

*/5 * * * * flock -w 60 /var/lock/sync-orders.lock /opt/jobs/sync-orders.sh

Skipping is usually right

If a job reads "everything new since last time", a skipped run costs nothing: the next one picks up the backlog. Waiting in line is what lets the pile-up grow.

Fix 2: a lock inside the script

When you cannot change the crontab, take the lock inside the script itself:

#!/usr/bin/env bash
set -euo pipefail

exec 9>/var/lock/sync-orders.lock
if ! flock -n 9; then
  echo "previous run still active, skipping" >&2
  exit 0
fi

# ... the real work ...

Avoid the pattern of creating a file at the start and deleting it at the end. If the script dies in between, the file stays and the job never runs again until someone notices. If you must do it, store the PID in the file and check whether that process is still alive.

Fix 3: locks that work across servers

flock only protects one machine. If the same cron entry runs on several servers, you need a shared lock:

  • PostgreSQL advisory locks. SELECT pg_try_advisory_lock(12345); returns false if another session holds the lock. It is released when the session ends.
  • Redis. SET lock:sync-orders token NX EX 600 sets the key only if it does not exist and expires it after 10 minutes, so a crashed holder cannot block forever.
  • Framework helpers. Many schedulers can run a task on one server only, or without overlapping, if you enable it. Check your framework's scheduler docs.

Always give shared locks an expiry that is longer than the longest normal run but short enough to recover from a crash.

Fix 4: put a ceiling on run time

A job that hangs holds its lock forever. Add a timeout so a stuck run is killed:

*/5 * * * * flock -n /var/lock/sync-orders.lock timeout 240 /opt/jobs/sync-orders.sh

Pick a timeout below the interval, then make the script safe to stop part-way: do the work in small transactions, or write to a temporary file and rename it when finished.

Fix 5: make the work idempotent

Locks reduce overlap, but they cannot cover every case, such as a manual run while the scheduled one is active. If running twice gives the same result as running once, collisions stop being dangerous. Mark rows as claimed in the same transaction that selects them, use unique keys for inserts, and write output atomically.

-- Claim a batch so a second worker skips it (PostgreSQL)
SELECT id FROM jobs
WHERE status = 'pending'
ORDER BY id
LIMIT 100
FOR UPDATE SKIP LOCKED;

Decide whether the job is too slow for its schedule

If a job regularly approaches its interval, guards only hide the problem. Consider:

  1. Making it cheaper: add an index, process only changed rows, or batch the work.
  2. Running it less often, if freshness does not require every five minutes.
  3. Moving it to a queue with a worker, so work is processed continuously rather than in bursts.
  4. Checking the schedule syntax with the cron expression generator.

Notice when runs are skipped or late

A lock that skips runs prevents damage, but it also means a stuck job can go unnoticed: every later run quietly skips. You want an alert when the job has not completed recently, not just when it has started.

That is what a heartbeat does. Have the job call a URL as its last step, and alert when the call does not arrive in time. In Spot Downtime, cron job monitoring expects a ping at your chosen interval plus a grace period, and you can call the ping URL with /fail to report an error straight away. Heartbeat monitors are available on the Starter and Pro plans. The guide to monitoring cron jobs shows the setup, and cron job not running helps if the job never starts at all.

*/5 * * * * flock -n /var/lock/sync-orders.lock /opt/jobs/sync-orders.sh && curl -fsS -m 10 https://example.com/ping/YOUR-TOKEN

Because the ping is chained with &&, it fires only when the job exits successfully. Skipped runs do not ping, so a job that stays locked for too long raises an alert once the grace period passes.

Cron expression explainerCheck what your cron schedule actually means, in plain language.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading