LinuxCommandLibrary

dotlockfile

Create and manage mutual exclusion lockfiles

SYNOPSIS

dotlockfile [-h] [-!nrRYptl] [-m<mode>] [-p<pidfile>] [-u<user>[:<group>]] <file>

PARAMETERS

-h
    Print short usage message and exit

-!
    Remove stale lock after querying user

-R
    Remove stale locks regardless of age

-Y
    Remove stale locks without querying

-n
    Fail immediately if lock unobtainable (non-blocking)

-rN
    Retry N times to obtain lock (default 10)

-tS
    Timeout after S seconds (default 5)

-lS
    Timeout for lock removal after S seconds

-pPIDFILE
    Write process ID to PIDFILE

-mMODE
    Set lock file permissions to MODE after creation

-uUSER[<GROUP>]
    Set lock file ownership to USER[:GROUP]

DESCRIPTION

The dotlockfile command is a utility designed to safely create, test, and remove dotlock files, which are simple locking mechanisms commonly used by mail user agents (MUAs) like procmail, mutt, and others to prevent concurrent access to mailbox files.

Dotlock files are named after the target file with a .lock suffix appended (e.g., mbox.lock for mbox). dotlockfile performs an atomic rename operation to create the lock, ensuring no race conditions. It supports retrying on failure, timeouts, handling stale locks, and setting permissions or ownership on the lock file.

This tool is particularly useful in mail processing environments where multiple processes might attempt to read/write the same mailbox simultaneously, avoiding data corruption. It does not provide kernel-level locking like flock, but relies on filesystem atomicity, making it suitable for local filesystems but with limitations on networked ones like NFS.

CAVEATS

Not NFS-safe due to reliance on atomic rename; use kernel locks like flock for networked filesystems. Stale lock detection uses file age heuristics, not foolproof.

EXIT STATUS

0 on success, 1 if lock acquired but later failed, 4 if could not create lock, 10 if stale lock removed.

FILES

Creates <file>.lock; respects $TMPDIR for temporary operations.

HISTORY

Developed by Stephen R. van den Berg for procmail in the early 1990s; remains part of procmail package, widely used in Unix mail systems.

SEE ALSO

procmail(1), mutt(1), lockfile(1), flock(1)

Copied to clipboard