LinuxCommandLibrary

flock

Serialize access to a file or resource

TLDR

Run a command with a file lock as soon as the lock is not required by others

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

Run a command with a file lock, and exit if the lock doesn't exist
$ flock [path/to/lock.lock] --nonblock --command "[command]"
copy

Run a command with a file lock, and exit with a specific error code if the lock doesn't exist
$ flock [path/to/lock.lock] --nonblock --conflict-exit-code [error_code] -c "[command]"
copy

SYNOPSIS

flock [options] file|directory command [arguments...]
flock [options] file|directory

PARAMETERS

-s, --shared
    Acquire a shared lock. Multiple processes can hold shared locks on the same file simultaneously.

-x, --exclusive
    Acquire an exclusive lock (default behavior). Only one process can hold an exclusive lock at a time.

-u, --unlock
    Remove an existing lock. This is typically used for explicit unlocking or clean-up.

-n, --nb
    Non-blocking mode. Fail immediately with a non-zero exit status if the lock cannot be obtained.

-w seconds, --wait seconds
    Wait for a specified number of seconds to acquire the lock. Returns failure if the timeout is reached.

-E seconds, --timeout seconds
    Alias for --wait.

-o, --close
    Close the file descriptor of the locked file after acquiring the lock and before executing the command. The lock remains active.

-c command
    Command to execute while holding the lock. This is the default behavior if a command follows the file argument.

-h, --help
    Display help message and exit.

-V, --version
    Display version information and exit.

DESCRIPTION

The flock command provides a mechanism for managing advisory locks on files or directories. It is an indispensable tool in shell scripting for ensuring inter-process synchronization and preventing race conditions. By applying a lock, flock helps guarantee that only one process (or multiple processes with shared locks) can access a critical section or shared resource at a time, thus maintaining data integrity. flock supports two types of locks: shared (allowing multiple readers) and exclusive (allowing only one writer). When flock is used to execute a command, the lock is held for the duration of that command's execution and is automatically released upon its completion. It also offers options for non-blocking lock attempts and setting timeouts for lock acquisition, making it flexible for various scripting needs.

CAVEATS

  • Advisory Locks: flock implements advisory locks, meaning processes must explicitly cooperate by also using flock to respect the lock. Non-cooperating processes can still access the file.
  • Deadlocks: Improper use can lead to deadlocks if multiple processes attempt to acquire locks in a circular fashion.
  • File Descriptors: Locks are associated with open file descriptors. If the file descriptor is closed (e.g., when the command exits), the lock is automatically released. Be mindful of how -o works.
  • Filesystem Support: Locks may not work reliably or as expected on all network filesystems (e.g., NFS), as they might not fully support flock(2) semantics.

<B>LOCKING MECHANISM</B>

flock operates by associating a lock with a specific open file descriptor for a given file or directory. The actual file content is not modified by the lock itself; instead, the kernel tracks the lock status. The lock is automatically released when the file descriptor is closed (e.g., when the process holding the lock terminates or explicitly closes the file) or when another lock is applied to the same file descriptor.

<B>EXIT STATUS</B>

If flock successfully acquires the lock and executes a command, its exit status will be that of the command it executed. If flock itself fails to acquire the lock (e.g., due to -n or -w timeout) or encounters an error, it will return a non-zero exit status.

HISTORY

The flock utility is a standard component of Unix-like operating systems, derived from the flock(2) system call first introduced in 4.2BSD. Its primary purpose is to provide a simple, portable, and widely adopted method for inter-process synchronization using file-based advisory locks. It gained significant usage in shell scripting for managing access to shared resources and preventing concurrent execution of critical tasks.

SEE ALSO

fcntl(2), lockf(3), open(2), mount(8)

Copied to clipboard