CronNudge

Published October 21, 2026

How to Prevent Overlapping Cron Job Runs

Two copies of a backup, a billing job, or a mailer are worse than one slow copy. They write the same file, send the same invoice, or fill the disk together. Cron does not stop them. A line that is due again while the previous process is still alive starts another process. If two runs start at the same scheduled time rather than one running long, that is a second schedule, covered in cron job running twice.

This page is why that happens, the locks that stop it on one machine and on a cluster, and the CronNudge signal that tells you a second start arrived anyway. A job that never starts at all is how to monitor cron jobs. A crontab that does not fire is crontab not running. The fall-back hour is cron jobs and daylight saving time. The dump wrapper that takes the lock before /start is monitor database backup cron jobs.

flock below is util-linux 2.38 flock(1), and the daylight-saving sentences are the same cron(8) rules as that daylight-saving page. Kubernetes behavior is the CronJob documentation, also written out in how to monitor Kubernetes CronJobs. The ping behavior is CronNudge's.

Why a second copy starts

What you see What actually happened
The run takes longer than the interval The next minute matches while the first process is still alive. Vixie cron starts another one.
Two hosts, or two replicas, have the same crontab Each daemon has its own clock and its own process list. Both fire.
A fixed-time job on the fall-back morning Debian cron and cronie do not re-run that fixed time. A wildcard schedule does run on both passes of the repeated hour. A local-time Kubernetes CronJob may create a second Job.
A retry The Job, the script, or curl itself started the work or the ping again. Those are different retries.

The run is longer than the interval

Cron wakes every minute and starts whatever matches. It does not look at last night's process. A */5 job that takes six minutes is still running at the next slot, and the next slot starts a second copy. A daily job that runs for 26 hours does the same thing the next night.

flock -n on one machine is the usual fix. It does not help a second machine. BusyBox crond is the exception to the double start: it skips the new process while it still considers the previous one to be running, and logs process already running. That skip does not ping. The flag and the log line are in monitor cron jobs in Docker.

Two hosts or two replicas

A crontab copied onto two servers runs twice. A container image with cron inside it does the same thing once the Deployment has two replicas: each pod has a daemon, and each daemon starts the job. A lock file inside the container is a lock on that container's filesystem. The other pod has a different file.

Kubernetes concurrencyPolicy applies to a CronJob, not to a Deployment that happens to run cron -f. The Deployment case is the two-host case.

A Spring @Scheduled method is the same shape once more than one copy of the application is running. Each process that enables scheduling runs the method on its own scheduler. Two instances run it twice. ShedLock is a separate library, not part of Spring, and a lock there is what makes the second instance skip. The details are in monitoring Spring scheduled tasks.

A Quartz job class marked @DisallowConcurrentExecution does not start a second execution of that job while one is still running. The job store sets the other triggers for that job to BLOCKED until the running execution finishes. A clustered JDBC job store is the other case: org.quartz.jobStore.isClustered makes the nodes share one database, and one node fires each time. The details are in monitoring Quartz Scheduler jobs.

A Hangfire recurring job is the other shape. Every server runs the recurring scheduler, and a distributed lock means one of them enqueues the occurrence. The background job can still run more than once, because processing is at least once. DisableConcurrentExecution is the lock Hangfire ships for that method, and the timeout is the number of seconds you pass. The details are in monitoring Hangfire recurring jobs.

An APScheduler job uses max_instances and coalesce. The built-in defaults are max_instances 1 and coalesce true. A new run is skipped, and the scheduler logs that the maximum number of running instances was reached, while an older instance of that job is still running. coalesce runs the latest due time once instead of every missed tick. The details are in monitoring APScheduler jobs.

Daylight saving time does not mean every job runs twice

The repeated hour is real civil time. What your cron does with it depends on the line and the daemon. This matches the daylight-saving page. Do not treat "the clocks fell back" as "the backup ran twice" without checking which of these you have.

  • A fixed minute and hour, with no * in either field (30 2 * * *, 30 1 * * *). Debian's cron(8) does not re-run jobs that fall in the repeated time when the clock moves back by less than 3 hours. cronie's cron(8) says running the same job twice is avoided. cronie's crontab(5) also says a time that occurs twice matches twice. That sentence is the field match. The jump itself is the cron(8) rule.
  • A wildcard or anything more often than hourly (*/15 * * * *, 0 * * * *, @hourly). These follow the real clock. A */15 job runs on both passes of the repeated hour, because those minutes occur twice. The second pass is about an hour of wall time later. It is a second start. It is usually not a second start while the first process is still in the first pass.
  • Kubernetes. The CronJob concept page does not document Debian's "do not re-run" rule. A report against 1.26, kubernetes/kubernetes#121594, describes a 0 2 * * * CronJob in Europe/Berlin creating a second Job when 02:00 occurred again. The issue was closed not-planned. Treat a local-time CronJob in the repeated hour as a job that may start twice.

Spring-forward is the other side. Debian's cron(8) runs a fixed-time job from the skipped hour soon after the change. cronie's cron(8) runs it immediately when the clock moved one hour forward. That catch-up can land on top of a run that is still going. Wildcard jobs are not caught up.

A CronNudge cron check expects a fixed-time expression once on that morning. A second success ping does not open another slot and does not alert by itself. Overlap is a different signal, below. The worked mornings are on the daylight-saving page.

Retries are two different things

A Kubernetes Job retries a failed Pod up to backoffLimit (the default is 6). Each retry is a new container. If the first container pinged /start and died before success or /fail, the retry's /start is a second start while the run is still open.

curl's --retry is not that. It sends the same HTTP request again. If the server stored the first /start and the response never reached curl, the retry is a second /start too. An Idempotency-Key header makes that HTTP retry a replay. The same key must not be reused for a real second run, and it must not be shared between /start and the finish ping. The details are in the CronNudge section.

flock

util-linux flock(1) takes an advisory flock(2) lock for the duration of a command, or on a file descriptor you already opened. The lock is released when the descriptor is closed, including when the process exits or is killed. A lock file left on disk is not a lock still held.

Non-blocking, the form to put on a crontab line. job.sh runs only if the lock is acquired now. If it is not, job.sh does not start, and flock exits 1.

flock -n /var/lock/job.lock /usr/local/bin/job.sh

Wait up to 30 seconds, then fail the same way. Decimal seconds are allowed. flock -w 0 is the same as -n. Do not pass -n and -w together. One of them is the wait policy.

flock -w 30 /var/lock/job.lock /usr/local/bin/job.sh

-E 75 changes the exit status used when -n or -w cannot get the lock. The default is 1, and the number has to be from 0 to 255. If the command does run, flock exits with the command's status, not with 1.

The file is created if it is missing. The directory is not. /var/lock has to exist and be writable by the user cron runs as. On many systems it is a symlink to /run/lock and is cleared on reboot. /tmp/job.lock is the other usual path. The path is the name of the lock. Two jobs that must not overlap use one path. Two jobs that may overlap use two paths.

The file-descriptor form

The manual's script form holds the lock for the subshell, then drops it when the subshell exits:

(
  flock -n 9 || exit 1
  /usr/local/bin/job.sh
) 9>/var/lock/job.lock

9> creates the file if needed and truncates it. The manual says the open mode does not matter to flock. > and >> both create the file and need write permission. < needs the file to exist already and needs only read permission. Truncating the file does not release a lock. The contents are not the lock.

exec 9> in the script itself holds the descriptor until that shell exits, which is what you want when the lock should cover every command in the file, including the pings. The backup script uses that form. Closing fd 9, or leaving the subshell, drops the lock.

-o closes the lock descriptor before the command runs, so a child the command spawns does not inherit it. The flock process still holds the lock until the command returns. A job that daemonizes (the process you started exits, and a grandchild keeps running) is then no longer covered: flock sees its child exit and releases the lock. -F does the opposite. It replaces the flock process with the command, which keeps the descriptor. -F and -o cannot be combined, because nothing would hold the lock.

What a leftover file means, and what deleting it does

flock(2) locks the open file, not the directory entry. After a crash the file is still there and the lock is gone. The next run creates or opens that name and locks it. You do not clear a flock by deleting the file, and you do not detect a live flock by testing that the file exists.

Deleting the name while a process still has it open removes the directory entry and leaves the lock on the old inode. The next flock creates a new file at that path and locks the new file. Both processes then hold "the" lock. Do not rm the lock file in a cleanup step.

A hand-rolled lock file is a different mechanism, and it does go stale:

if [ -e /var/lock/job.lock ]; then
  echo "already running" >&2
  exit 1
fi
echo $$ > /var/lock/job.lock
/usr/local/bin/job.sh
rm -f /var/lock/job.lock

If the shell is killed between echo and rm, the file remains and every later run exits. A PID written into the file can be reused by an unrelated process, so a check of "is that PID alive?" can skip a run that should start, or start a run that should wait. flock does not have that failure. The PID file does.

flock(1) says some filesystems, including NFS and CIFS, implement flock(2) only partly, and flock can fail every time depending on the mount. flock(2) says that since Linux 2.6.12 an NFS client emulates flock as an fcntl lock on the whole file, so an exclusive lock needs a writable open, and that local_lock on the mount can keep the lock local. A lock file on a shared disk is not a distributed lock until you have tested that two hosts actually exclude each other. If they do not, both jobs run.

Kubernetes CronJob

concurrencyPolicy is on the CronJob, and it decides what happens when the next schedule is due and a Job from this CronJob is still active.

  • Allow is the default. The new Job is created. The previous Job keeps running. Both pods run your command.
  • Forbid does not create the new Job while the previous one is still active. A Pod stuck Pending counts as active, and it does not move backoffLimit. Later schedules are skipped until that Job finishes or activeDeadlineSeconds on the Job ends it.
  • Replace deletes the running Job and starts a new one. The killed container often never reaches /fail.

Forbid is the right policy for a backup that must not overlap. It is also how one stuck Job silences every later schedule. The skip is a missed schedule. Kubernetes counts missed schedules, and more than 100 since the last scheduled time produces too many missed start times and no catch-up Job. If startingDeadlineSeconds is set, only misses inside that window are counted.

.spec.startingDeadlineSeconds is how late, in whole seconds, this occurrence may still start. After that, this occurrence is skipped. Later ones are still scheduled. The controller looks about every 10 seconds, so a deadline under 10 seconds may never fire. When Forbid skipped a run and the previous Job then finishes, the deadline still applies, and the skipped time may already be too late to start.

startingDeadlineSeconds: 600 on a daily CronJob allows that Job to be created up to 10 minutes late. It does not stop two Jobs if the policy is Allow. The manifest, the 100-miss example, and activeDeadlineSeconds are in the Kubernetes guide. CronNudge does not read the policy. It sees pings. Forbid means the second pod never starts, so it never pings /start, and there is no overlap alert. The alert you get is the missing success ping if the first Job never finishes.

An Argo CronWorkflow uses the same three values on concurrencyPolicy. Allow is the default and creates the new Workflow beside the active one. Forbid skips the new Workflow while one is still active, and a Workflow that is still Pending counts. Replace stops the active Workflows, then creates the new one. The details are in monitoring Argo CronWorkflows.

A lock across hosts

flock on one machine, and concurrencyPolicy on one CronJob, cover the cases above. Two app servers that both run the crontab need a lock both of them can see.

A Postgres session lock such as pg_try_advisory_lock is held by that database session and drops when the session ends, including a crash that closes the connection. A Redis SET key token NX PX <milliseconds> is a lease: it expires even if the holder is alive. If the job runs longer than the TTL, or the process pauses past it, a second worker takes the key and both run. Deleting the key without checking the token deletes another worker's lock. etcd leases have the same shape. The lease has to be longer than a healthy run, and the work still has to be safe when the lease is wrong.

The job itself should be safe to run twice. A digest should key off the calendar date, not off "I was invoked." A dump should write a temp file and mv it into place, so two writers do not leave one truncated file. The lock reduces how often the second run happens. The idempotent write is what remains when the lock does not.

What CronNudge records

The check's ping URL is https://cronnudge.com/ping/<check-id>. GET, POST, and HEAD all count. Store it in CRONNUDGE_URL with no query string and no /start suffix. /start and /fail are that id with a path. The response is text/plain ok with Cache-Control: no-store. curl -fsS does not print the URL.

Turn on "Track start and finish" on the check. Setting Max runtime (seconds) or Duration deviation % turns run tracking on as well. Overlap is only evaluated when run tracking is on.

/start records a start. It is not a success. A second /start while a run is open sends an overlap alert. The alert text is that the check started again before the previous run finished. That second start does not delete the first run. Both stay open until a success ping or /fail, and that finish ping closes every open run for the check. Overlap does not, by itself, mark the check down. A paused check records the ping and does not alert.

The max-runtime clock moves to the latest /start. Allowed values are 1 second through 7 days. When it is set, the deadline is that start time plus that many seconds, and the sweep marks the check stuck: it is still running, and it passed its max runtime without a finish ping. The alert fires once for that start. A success ping is the recovery.

With run tracking and no max runtime, an open run is due after the grace period, and that wait is at least 60 seconds. On a nightly schedule the next cron slot is tomorrow, so the open-run deadline is the one that fires. With the default 5-minute grace, a job that takes 20 minutes is marked down while it is still running. That down alert is "no successful ping arrived," not the stuck alert. Set Max runtime longer than a healthy run when you ping /start. How to see that the process is still there, and how GNU timeout, systemd RuntimeMaxSec=, or a Kubernetes activeDeadlineSeconds ends it, is cron job stuck or hanging.

Duration deviation % compares the new duration with the median of earlier successful runs, after at least three earlier samples, using up to the last 20. The duration is the time from the latest /start to the success ping. The alert says the run took longer or shorter than recent runs. It does not, by itself, mark the check down. The percent has to be from 1 to 1000.

/fail marks the check down and alerts immediately when it was not already down, and it closes the open runs. That is why the copy that loses the lock must not call /fail. It would close the run the first copy still has open. Exit without pinging. The first copy's missing finish is the stuck or late alert.

An Idempotency-Key header is stored on the check, not per path. The same key on a later request is a replay: no second ping and no overlap alert. A retried /start should reuse its own key. The success or /fail request needs a different key, or none. A real second run of the job needs a new key, or the second /start is swallowed and you will not get the overlap alert. Keys are truncated at 128 characters.

Free includes output assertions and run tracking on 3 checks. Pro removes that cap. Token and dollar limits are a different feature, on Pro, and this check does not use them. Check limits stay 10 on Free and 100 on Pro. Free's shortest interval is 5 minutes. Pro is $12 a month and its shortest interval is 1 minute. The numbers are on pricing. The field list is assertions, runs, and agents.

Take the lock before /start. A second copy that cannot get the lock exits without pinging.

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

url="${CRONNUDGE_URL:?CRONNUDGE_URL is not set}"

ping() {
  curl -fsS -m 10 --retry 3 -o /dev/null \
    -H "Idempotency-Key: ${HOSTNAME:-local}-$$-$1" \
    "$2" || true
}

mkdir -p /var/lock
exec 9>/var/lock/job.lock
if ! flock -n 9; then
  echo "job.lock is already held" >&2
  exit 1
fi

ping start "${url}/start"
if /usr/local/bin/job.sh; then
  ping ok "$url"
else
  status=$?
  ping fail "${url}/fail?exit=${status}"
  exit "$status"
fi

The key is the shell's host name, the pid, and the phase, so a curl retry of /start replays and a later success does not reuse that key. A second host has a different hostname and will still ping /start if it does not share this lock file. That second start is the overlap alert. On one machine, a kill that skips the rest of the script leaves the run open. Max runtime marks it stuck. The next schedule's /start can also overlap, because the run stays open until a success or /fail.

?exit= on /fail is stored as the exit code. A non-zero ?exit= on the success URL is stored as a failure. Use the /fail path in the failure branch.

Default grace is 5 minutes. The form offers none, 1 minute, 5 minutes, 15 minutes, and 1 hour. The API accepts any whole number of seconds from 0 through 7 days. Grace is how late the schedule may be. It is the wrong knob for a long run if you also ping /start, unless max runtime is set. Either ping only at the end and set grace longer than the run, or ping /start and set max runtime.

Match the check's expression and timezone to the crontab. Compare the five fields on the cron tool. One check per job. A shared URL stays up while any other job still pings.

FAQ

Why does a cron job start a second copy? The next schedule matches while the previous process is still running, or a second host or replica has the same crontab. Vixie cron does not track the previous process. A fixed-time job is not re-run by Debian cron or cronie when the clock falls back. A wildcard schedule does run on both passes of the repeated hour.

Does flock leave a stale lock after a crash? No. The lock is released when the file descriptor closes, and a crash closes it. The file can remain. The file is not the lock. Deleting the file while a process still has it open lets the next run lock a new file and both run. A PID file that you test with [ -e ] does go stale.

Does concurrencyPolicy Forbid send an overlap alert? No. Forbid does not start the new Job, so nothing pings /start. CronNudge overlap runs only when a second /start arrives while a run is open and run tracking is on. With Forbid, a Job that never finishes shows up as a missed success ping, or as a stuck run if you pinged /start and set a max runtime.

Does a fall-back morning run a 2:30 cron twice? Not on Debian cron or cronie, for a fixed minute and hour. Those daemons do not re-run the time that falls in the repeated hour. The repeated hour in New York is 1:00–1:59, not 2:00. A wildcard schedule does run on both passes. A local-time Kubernetes CronJob may create a second Job. A second success ping on a fixed-time check does not alert by itself.

Does a second /start erase the first run? No. With run tracking on, the second /start alerts overlap and leaves the first run open. A success ping or /fail then closes every open run. /fail from the copy that lost the lock would close the run that is still working. Max runtime is measured from the latest /start.

Do overlap alerts require the Pro plan? Run tracking, which overlap, max runtime, and duration deviation use, is on Free for 3 checks. Pro removes that cap. The check count is separate: 10 on Free, 100 on Pro. Pro is $12 a month. Token and cost alerts are Pro, and a cron overlap check does not use them.


The lock belongs in the job. The overlap alert is how you hear that a second start got past it. Create the check, or read how to monitor cron jobs if the schedule itself never fires. Related: Kubernetes CronJobs, cron in Docker, and assertions, runs, and agents.