flock
manage advisory file locks from shell scripts
TLDR
Run command with file lock
SYNOPSIS
flock [options] file|directory command [arguments]
flock [options] file|directory -c command
flock [options] number
DESCRIPTION
flock manages file locks from shell scripts. It executes a command while holding a lock on a specified file or directory, creating the lock file if needed. By default, the command waits indefinitely for the lock to become available. This is useful for ensuring only one instance of a command runs at a time.
PARAMETERS
-c, --command
Execute a single command string via the shell-e, -x, --exclusive
Obtain an exclusive (write) lock (default)-s, --shared
Obtain a shared (read) lock-n, --nb, --nonblock
Fail immediately if the lock is not available-w, --wait, --timeout SECONDS
Fail if the lock is not acquired within specified seconds-o, --close
Close the file descriptor before executing the command-u, --unlock
Manually drop the lock-E, --conflict-exit-code NUM
Exit code when lock fails with -n or timeout (default: 1)-F, --no-fork
Do not fork before executing the command--verbose
Report how long the lock took to acquire
CAVEATS
flock does not detect deadlock situations. File locking has limited support on certain network filesystems like NFS and CIFS. When the -n or -w option is used and the lock cannot be acquired, flock exits with the code specified by -E (default 1).
HISTORY
flock is part of the util-linux package, providing shell-level access to the flock(2) system call.
