How to Run a Cron Job on Your VPS Without Overlapping Runs

A cron job that takes longer than its schedule allows is a quiet way to break a server. The next run starts while the previous one is still working, two copies of the same script fight over the same files, and a backup that should take ten minutes ends up running three times at once. This guide shows how to run a cron job without overlapping runs on your VPS, using flock, a lock file, or a systemd timer. You only need SSH access and a text editor.

Why overlapping runs happen

Cron has no memory. It fires a command at the scheduled time whether the last run finished or not. That is fine for a job that takes seconds, but it turns into a real problem the moment a job takes longer than its interval. A backup script that normally takes four minutes can take forty when the disk is busy or the network is slow, and if it runs every five minutes, the second copy starts before the first one is done.

The damage depends on the job. Two backup scripts writing to the same archive can corrupt it. Two database dumps can double the load on a small VPS until MySQL stalls. Two copies of a script that sends email can send the same message twice. WordPress sites have their own version of the same problem: a WordPress cron event that fires on every visit can stack up the same way when traffic spikes. The fix is the same in every case: make sure only one copy of the job can run at a time.

Use flock to run a cron job without overlapping runs

flock is the simplest answer on most Linux servers, and it is already installed on Ubuntu and Debian as part of util-linux. It takes a lock on a file before running your command, and with the -n flag it exits at once if another copy already holds the lock. No lock file cleanup, no extra script. A flock cron job is the fastest fix for a job that already exists and just needs a guard around it.

A crontab line looks like this:

*/5 * * * * /usr/bin/flock -n /var/lock/backup.lock /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

Three details matter here. First, -n means non-blocking: if the lock is held, flock exits right away instead of queuing behind the running copy. Second, the cron lock file lives in /var/lock, a directory meant for exactly this, and the file itself can be empty; flock only cares about the lock on it. Third, use full paths for every command, because cron runs with a minimal PATH and a bare flock may not resolve.

Test it before you trust it. Start the script once in a terminal, then run the same flock line in a second terminal while the first copy is still going. The second copy should exit immediately with no output. Check with ps aux | grep backup.sh and you should see one process, not two.

If you would rather wait than skip, swap -n for -w 300. flock then waits up to five minutes for the lock and only then gives up. That suits a job that must run eventually but must never run twice, such as a nightly MySQL backup that pulls a fresh dump before rsync moves it off the server.

Use a lock file with a stale-lock timeout

If flock is not available, or you want the check inside the script itself, use an atomic directory as the lock. mkdir either creates the directory or fails, and that either-or is what makes it safe: two copies can never both succeed.

LOCKDIR=/var/lock/myjob.lock
if ! mkdir "$LOCKDIR" 2>/dev/null; then
  echo "Already running, exiting." >&2
  exit 0
fi
trap 'rmdir "$LOCKDIR"' EXIT

The trap matters. It removes the lock when the script exits, even if it fails halfway. Without it, a crashed job leaves a lock behind and the job never runs again. If a crash could still leave the directory behind, write the script’s PID into a file inside the lock directory, and on the next run check whether that PID is still alive before deciding the lock is stale. A PID that no longer exists means the old run is gone and the lock is safe to remove; a live PID means the job is genuinely still running.

Switch to a systemd timer when you want the scheduler to handle it

If your VPS runs systemd, a timer is the cleanest option for jobs that must never overlap. A timer triggers a service, and systemd does not start a second copy of a service that is already running. The skipped run shows up in the journal, so you can see it happened instead of guessing. Our walkthrough on systemd timers covers the full setup, but a minimal timer unit looks like this:

[Unit]
Description=Run backup every 5 minutes

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

The trade-off is setup time. A timer needs a service unit plus a timer unit and a systemctl enable, where flock needs one flag in a crontab line. Pick timers when you already manage services with systemd and want logs in journalctl. Stick with cron and flock for quick jobs you want guarded in under a minute.

Log every run so overlaps stop being invisible

A lock stops the overlap, but logging proves it worked. Append each run to a file with a timestamp and record the exit code, then check the log the next morning: one start line per interval, no doubled entries. If a job keeps getting skipped because the previous run never finishes, the log is where you find out, not a corrupted backup a week later.

Logs from a job that runs every five minutes grow fast, so set up log rotation before the file eats your disk. The same lock pattern applies to a daily server health report cron job that mails you load, disk use, and failed services each morning, and that report is how you notice a job that keeps getting skipped.

Which method should you pick?

Method Setup Stale lock risk Best for
flock -n One flag in crontab None, the lock dies with the process Most cron jobs
flock -w One flag in crontab None Jobs that must run eventually
mkdir lock in script A few lines in the script Yes, needs a PID check Scripts you control end to end
systemd timer Two unit files None Servers already managed with systemd

Start with the job most likely to overrun, usually the backup. Wrap it in flock -n today, check the log tomorrow, and only then move on to the next job. One lock per job, one log per job, and your cron jobs run without overlapping runs.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.