FLOCKSection: User Commands (1)
Updated: November 2004
Index Return to Main Contents
NAMEflock - acquire a file lock and then execute a command with the lock held
SYNOPSISflock [ --shared | --timeout=seconds ] lockfile command ..
Acquire a file lock using the flock(2) system call and then execute the given command with the lock held. Depending on the options given, the lock can be either exclusive or shared, and the behavior in the event of lock contention can be specified as either waiting indefinitely for the lock to become available (the default), or failing if the lock does not become available after a specific time, which can be specified as zero to make the command not wait at all.
- Acquire a shared lock. Acquiring a shared lock does not stop others from acquiring a shared lock, but it will stop others from acquiring an exclusive lock. Conversely, acquiring an exclusive lock (the default) stops both exclusive and shared attempts to acquire the lock. Typically, a shared lock is used if a command is just going to read the locked data, and an exclusive lock is used if the command might write to it.
- Abort if the lock cannot be acquired before n seconds. For a completely non-blocking attempt to acquire a lock, specify --timeout=0. The timer applies only to the attempt to acquire the lock. As soon as the lock is acquired, the timeout is cancelled. The command to be run is not subject to the timeout.
EXAMPLES (invoking some imaginary programs)
flock /etc/passwd read-and-write-to-passwd
flock --shared /etc/passwd just-read-something-from-passwd
AUTHORWritten by Adam J. Richter