Payments are experiencing issues due to temporary restrictions in Russia. If your payment does not go through, please submit a support request.Our support team is available 24/7 — we are always here to help with hosting and server issues.We are now accepting requests for dedicated server rental and colocation services in our data center.Reminder: we recommend enabling backups for additional data protection.A new VPS/VDS lineup with NVMe storage and improved performance is now available.Maintenance work on some servers has been completed. All services are operating normally.
Article10 min readViews3

flock: How to Prevent Concurrent Backup Execution

We test on an isolated stand how flock stops a second backup run, how a lock conflict differs from a command error, and why a leftover lock file proves nothing.

Comments 0

Two backup modules before a single protected server room passage
In this article

Backup may take longer than usual: data volume has grown, storage has slowed, or a network route has hung. If cron, a systemd timer, or an operator launches the same task again at that moment, two processes will start reading, packaging, or offloading the same data. The result may not necessarily be corrupted, but checking load, logs, and temporary file cleanup becomes harder.

Short answer: wrap the entire critical part of the job with a single exclusive flock lock, enable non-blocking mode, and assign a distinct conflict code. Then verify three events: the first run acquires the lock, the second run exits with a conflict code while the lock is held, and the third run succeeds after the lock is released. Below is such a test with actual results.

What exactly flock protects

The flock utility from util-linux manages file or directory locking and can hold the lock for the duration of a command. By default, it creates an exclusive lock. The --nonblock parameter causes a second run to exit immediately without waiting, while --conflict-exit-code sets a distinct exit code for this event. This behavior is documented in the util-linux manual and on the flock(1) man page.

The lock is advisory: it works only if all competing processes use the same lock object. No one can stop a process that bypasses flock or points to a different file. Moreover, the lock itself does not guarantee that the backup is complete or restorable; it only addresses concurrent access to the critical section.

At the kernel level, the lock is tied not to the file name but to the open file description. It is released only when the associated file descriptors are closed. Therefore, the existing backup.lock cannot be taken as proof of a running process. This important distinction follows from the flock(2) description.

First, test on an isolated staging environment

The check was performed on November 15, 2026, on Debian GNU/Linux 13 (trixie), kernel 6.18.57, util-linux 2.41.5, and Bash 5.2.37. The temporary directory was located on tmpfs in /tmp. The script produced the same result twice and exited with code 0. This is a test of a local file system, not NFS, CIFS, cron, systemd, or a specific backup program.

The scenario creates a separate directory of the form /tmp/flock-lab.XXXXXX. All paths are defined within it. Cleanup is allowed only for this template; if the variable unexpectedly points elsewhere, the script will refuse to delete the data.

Save the following text to file flock-lab.sh in a separate test directory. To run it, a regular user with write permissions to /tmp is sufficient; root access is not required. The system must have Bash installed, util-linux with the flock utility, and standard commands mktemp, grep, sleep, chmod, cat, and rm. The script waits for the string "first acquired" for no more than five seconds; if the first process does not write it, the check fails with an error and does not conclude that a conflict occurred.

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

lab_dir="$(mktemp -d /tmp/flock-lab.XXXXXX)"
lock_file="$lab_dir/backup.lock"
log_file="$lab_dir/run.log"
worker="$lab_dir/worker.sh"

cleanup() {
if [[ "$lab_dir" =~ ^/tmp/flock-lab\.[A-Za-z0-9]{6}$ ]]; then
rm -rf -- "$lab_dir"
else
printf 'Refusing cleanup outside the lab: %s\n' "$lab_dir" >&2
fi
}
trap cleanup EXIT

cat >"$worker" <<'WORKER'
#!/usr/bin/env bash
set -euo pipefail

lock_file="$1"
log_file="$2"
label="$3"
hold_seconds="$4"

exec 9>"$lock_file"
if flock --exclusive --nonblock --conflict-exit-code 75 9; then
:
else
lock_rc=$?
if [[ "$lock_rc" -eq 75 ]]; then
printf '%s conflict exit=75\n' "$label" >>"$log_file"
else
printf '%s lock_error exit=%s\n' "$label" "$lock_rc" >>"$log_file"
fi
exit "$lock_rc"
fi

printf '%s acquired\n' "$label" >>"$log_file"
sleep "$hold_seconds"
printf '%s finished\n' "$label" >>"$log_file"
WORKER
chmod 700 "$worker"

"$worker" "$lock_file" "$log_file" first 2 &
first_pid=$!
ready=0
for attempt in {1..50}; do
if [[ -f "$log_file" ]] && grep -Fxq 'first acquired' "$log_file"; then
ready=1
break
fi
if ! kill -0 "$first_pid" 2>/dev/null; then
break
fi
sleep 0.1
done
if [[ "$ready" -ne 1 ]]; then
wait "$first_pid" || :
printf '%s\n' 'First worker did not report lock acquisition in time' >&2
exit 1
fi

set +e
"$worker" "$lock_file" "$log_file" second 0
second_rc=$?
set -e

wait "$first_pid"

"$worker" "$lock_file" "$log_file" third 0
third_rc=$?

set +e
flock --exclusive --nonblock --conflict-exit-code 75 "$lock_file" /bin/sh -c 'exit 23'
child_rc=$?
set -e

printf 'second_rc=%s\n' "$second_rc"
printf 'third_rc=%s\n' "$third_rc"
printf 'child_rc=%s\n' "$child_rc"
printf 'lock_file_exists=%s\n' "$([[ -e "$lock_file" ]] && printf yes || printf no)"
printf '%s\n' '--- log ---'
cat "$log_file"

[[ "$second_rc" -eq 75 ]]
[[ "$third_rc" -eq 0 ]]
[[ "$child_rc" -eq 23 ]]
grep -Fxq 'first acquired' "$log_file"
grep -Fxq 'second conflict exit=75' "$log_file"
grep -Fxq 'first finished' "$log_file"
grep -Fxq 'third acquired' "$log_file"
grep -Fxq 'third finished' "$log_file"

Syntax check and execution:

bash -n flock-lab.sh
bash flock-lab.sh

How to read the result

In two sequential runs, the same output was observed:

second_rc=75
third_rc=0
child_rc=23
lock_file_exists=yes
--- log ---
first acquired
second conflict exit=75
first finished
third acquired
third finished

Event

Observation

Output

A: first

acquired the lock and ran for 2 seconds

critical section is occupied

B: second

finished with exit code 75

did not enter the critical section

C: third

after A completed, returned 0

lock released

Command with exit 23

flock returned 23

exit code of the wrapped command saved

Timeline: A holds the lock, B receives exit code 75, and C completes with exit code 0 after release
Verified sequence: B was rejected while A was running, and C acquired the lock after it was released.

Log lines matter more than a single code: between first acquired and first finished there is second conflict exit=75, but no second acquired. After first finished, third acquired appears. In other words, the second process truly did not enter the protected section, while the third entered after it was released.

The line lock_file_exists=yes is not an error. The lock file remains, but the third process successfully locked it. Deleting such a file "just in case" is not required: the presence of a file and holding a lock are different states.

A lock conflict and a backup error are distinct events

In the test, code 75 means only that the lock could not be acquired immediately. When the lock was free, command /bin/sh -c 'exit 23' executed, and flock returned the exit code of that command — 23. This allows monitoring to distinguish between "previous run is still in progress" and "the task itself ended with an error".

Code 75 is not universal. Before deployment, check the documentation for your backup software and wrapper script. If they already use 75, choose another available code in the 1–255 range and record it in monitoring. Otherwise, a real error may appear as a standard launch conflict. Zero is not suitable: monitoring must distinguish between a conflict and successful execution. The error code from the flock utility itself must not be renamed to a conflict: the script preserves its original non-zero code and logs a separate lock_error event.

Acceptance criterion: the conflict code belongs only to the lock, and the non-zero exit code of the backup command itself is not overwritten or treated as "still running".

How to migrate the check to the actual task

The laboratory script does not know the paths, credentials, or exit codes of your backup program. Therefore, you must migrate not its temporary names, but the verified sequence. I would conduct acceptance testing as follows:

  1. Find all entry points for a single task: cron, systemd timer, control panel, manual script, and external automation. They must all use the same locking mechanism.

  2. Select a stable local lock file in a directory where the service user can write but external users cannot replace its contents. Do not place it on a network file system without separate testing.

  3. Place the entire critical section under a single lock: creating a temporary copy, packaging, transferring, and the cleanup that must not overlap with a second run.

  4. Assign a conflict code that does not match the documented codes of the backup command. Log the lock acquisition, conflict, completion, and the command code separately.

  5. In the test environment, run A while it holds the lock, then run B, and after A finishes, run C. Verify not only the codes but also the absence of B actions in the destination directory.

  6. Simulate a safe error from the command itself and ensure its code reaches monitoring without being replaced by the conflict code.

  7. Only then migrate the schedule. Monitor the first operational cycle via logs and load metrics; the lock does not replace control over the backup result.

If the second launch should not terminate but wait, the non-blocking mode can be replaced with a limited wait. This is a different criterion: you must measure the allowable queue time and decide what to do after the timeout. An infinite wait can hide a hung previous task.

Solution boundaries

  • NFS and CIFS/SMB may implement or emulate locks differently; behavior depends on the kernel, server, and mount parameters. The local test above does not confirm this.

  • flock is not a distributed lock for multiple servers. If different nodes run the same task, a coordinator is required that all participants can see and whose failures are handled separately.

  • Child processes may inherit an open file descriptor and extend the lock's lifetime. The --close parameter changes this behavior, but it must not be enabled blindly: the lock can be released before the critical work is actually finished.

  • A process that does not use the same lock file will continue running. The lock path and wrapper must be identical for all launch points.

  • The lock does not verify archive readability, database completeness, or the ability to restore.

After protecting against parallel launches, separately verify the result itself. For this, check RPO and RTO and perform a test restore. If the copy base is a VPS snapshot, consider file and database consistency. If the archive was created with tar and the source directory is still accessible, you can compare the archive with the source directory before cleanup.

Acceptance testing criteria

Check

Expected result

A started first

acquired the lock and entered the critical section

B started while A was running

did not execute the protected work; returned a separate conflict code

C started after A

acquired the lock and exited with its own code

the command failed

its code reached monitoring and did not turn into a conflict

the lock file remained

the new process still acquires a free lock

The path is on NFS/CIFS

lock behavior remains unconfirmed until tested on this specific mount

It is premature to draw conclusions based on a single indicator. A separate lock file, a zero exit code from a single run, or the absence of two processes at the moment of inspection do not prove protection. Proof requires a reproducible A/B/C sequence, distinct exit codes, and a log showing that the second process did not perform critical work.

Discussion 0

Share your experience and ask questions. Comments without links appear after editorial review.

No comments yet. Start the discussion.