LinuxCommandLibrary

flock

Serialize access to a file or resource

TLDR

Run a command with a file lock as soon as the lock is available

$ flock [path/to/lock.lock] [command]
copy

Run a command with a file lock, or exit if the lock is currently being held (with exit code 1)
$ flock [path/to/lock.lock] [[-n|--nonblock]] [command]
copy

Run a command with a file lock, or exit with a specific error code if the lock is currently being held
$ flock [path/to/lock.lock] [[-n|--nonblock]] [[-E|--conflict-exit-code]] [123] [command]
copy

Run a command with a file lock, waiting up to 10 seconds for the lock to be available before giving up
$ flock [path/to/lock.lock] [[-w|--timeout]] 10 [command]
copy

Backup a bunch of files, waiting for the previous tar command to finish if it's still running elsewhere and holding the same lock file (can be used in a cron job that runs often)
$ flock [path/to/backup.lock] [tar -cvf path/to/backup.tar path/to/data/]
copy

SYNOPSIS

flock [-c command] [-n] [-o] [-s] [-u] [-v] [-w seconds] [-X] lockfile [command [args...]]

PARAMETERS

-c, --command command
    Run command in current shell after acquiring lock (no fork).

-n, --nonblock, --no-fork, --fail
    Fail (exit 1) instead of waiting for lock.

-o, --close
    Close lock FD before exec'ing command (for /var/lock SUID safety).

-s, --shared
    Acquire shared (reader) lock instead of exclusive.

-u, --unlock
    Drop existing lock and exit (auto on normal exit).

-v, --verbose
    Log PID and command to syslog.

-w, --wait, --timeout seconds
    Wait up to seconds before failing.

-X, --extension-allowed
    Allow lockfile extensions like .lock.

-h, --help
    Show help.

-V, --version
    Show version.

DESCRIPTION

Flock is a command-line tool from the util-linux package that wraps the flock(2) system call, enabling easy file locking in shell scripts. It acquires an exclusive or shared lock on a specified lockfile before executing a command, preventing concurrent access to shared resources like databases, log files, or devices.

This is essential for scripts run by cron, daemons, or parallel processes to avoid race conditions. For example, it ensures only one instance of a backup script runs at a time.

Lock semantics: Uses fcntl-style advisory locks on open file descriptors. Locks persist until the FD closes (via exec, close, or process exit). Supports non-blocking attempts, timeouts, and unlocking.

Invocation styles:
- Prefix: flock lockfile command args (forks, holds lock during child).
- Lockfile: flock lockfile (acquire/hold/release on exit).
- Inline: flock -c 'command' (no fork, runs in current shell).

Ideal for lock directories like /var/lock, where SUID issues arise without -o. Verbose logging aids debugging.

CAVEATS

Advisory locks: ignored by uncooperative processes. Released on FD close/exec/exit. Unreliable over NFS. Per-process FD, not file path. Use hard-linked lockfiles for directories.

EXAMPLES

Simple exclusive:
flock /tmp/mylock myscript.sh

Non-blocking:
flock -n lockfile || { echo 'Already locked'; exit 1; }
command args


Timeout:
flock -w 10 /var/lock/myservice myservice-daemon

Inline:
flock -c 'make && make install'

HISTORY

Introduced in util-linux 2.13 (2008) by Peter Anvin. Evolved with timeout (-w) in later versions. Now standard in util-linux-ng/util-linux for portable shell locking.

SEE ALSO

flock(2), fcntl(2), lockfile(1)

Copied to clipboard