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] [args...]
flock [options] -c

PARAMETERS

-s, --shared
    Obtain a shared lock. Multiple processes can hold a shared lock simultaneously, but no process can hold an exclusive lock at the same time.

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

-u, --unlock
    Manually unlock the file. This is generally unnecessary as the lock is automatically released when the process exits or the file descriptor is closed.

-n, --nonblock
    Fail immediately if the lock cannot be obtained. Useful for avoiding blocking when the resource is unavailable.

-w, --timeout
    Wait for a specified number of seconds to obtain the lock. If the lock cannot be acquired within the timeout period, exit with an error.

-o, --close
    Close the file descriptor on which the lock is held before executing the command. Useful for preventing child processes from inheriting the lock.

-c, --command
    Specify the command to execute under the lock. This allows executing a single command directly without a script.

-E, --exact-close
    Specifies that the file descriptor should be closed with close() rather than fclose().

DESCRIPTION

The flock command provides a simple mechanism for managing file locks from shell scripts or the command line. It allows you to ensure that only one process at a time can access a particular file or resource. flock works by creating a lock file (or using an existing file descriptor) and acquiring an exclusive or shared lock on it. Other processes attempting to acquire the same lock will block until the lock is released. This is useful for preventing race conditions and ensuring data integrity when multiple processes need to access the same resource. flock is particularly helpful for preventing cron jobs or background processes from interfering with each other. It's a lightweight alternative to more complex locking mechanisms and is widely available on Linux systems.

It's crucial to handle exit signals gracefully when using flock, ensuring that the lock is always released even if the process is interrupted. Proper error handling is essential to prevent deadlocks or other issues related to lock management.

CAVEATS

Flock only works on local file systems. Networked filesystems might need specific options, or other tools might be more suited.

SIGNAL HANDLING

It's important to handle signals (e.g., SIGINT, SIGTERM) gracefully when using flock. If the process is terminated abruptly without releasing the lock, other processes may be blocked indefinitely. Use a signal handler to ensure that the lock is released when the process exits, even if it's interrupted.

EXAMPLE

flock -n /tmp/mylock -c 'echo "Process $(($RANDOM%100)) running"' This command attempts to acquire an exclusive lock on the /tmp/mylock file and executes a command if the lock can be acquired in a non-blocking manner. If the lock can't be acquired immediately, the command does not run.

SEE ALSO

lsof(1), fuser(1)

Copied to clipboard