table of contents
FLOCK(1) | User Commands | FLOCK(1) |
NAME¶
flock - manage locks from shell scripts
SYNOPSIS¶
flock [options] file|directory command [arguments]
flock [options] file|directory -c command
flock [options] number
DESCRIPTION¶
This utility manages flock(2) locks from within shell scripts or from the command line.
The first and second of the above forms wrap the lock around the execution of a command, in a manner similar to su(1) or newgrp(1). They lock a specified file or directory, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, flock waits until the lock is available.
The third form uses an open file by its file descriptor number. See the examples below for how that can be used.
OPTIONS¶
-c, --command command
-E, --conflict-exit-code number
-F, --no-fork
-e, -x, --exclusive
-n, --nb, --nonblock
-o, --close
-s, --shared
-u, --unlock
-w, --wait, --timeout seconds
--verbose
-h, --help
-V, --version
EXIT STATUS¶
The command uses <sysexits.h> exit status values for everything, except when using either of the options -n or -w which report a failure to acquire the lock with an exit status given by the -E option, or 1 by default. The exit status given by -E has to be in the range of 0 to 255.
When using the command variant, and executing the child worked, then the exit status is that of the child command.
NOTES¶
flock does not detect deadlock. See flock(2) for details.
Some file systems (e. g. NFS and CIFS) have a limited implementation of flock(2) and flock may always fail. For details see flock(2), nfs(5) and mount.cifs(8). Depending on mount options, flock can always fail there.
EXAMPLES¶
Note that "shell> " in examples is a command line prompt.
shell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo; /bin/echo $?
shell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?
shell> flock -x local-lock-file echo 'a b c'
(; flock -n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :
shell> exec 4<>/var/lock/mylockfile; shell> flock -n 4
AUTHORS¶
H. Peter Anvin <hpa@zytor.com>
COPYRIGHT¶
Copyright © 2003-2006 H. Peter Anvin. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SEE ALSO¶
REPORTING BUGS¶
For bug reports, use the issue tracker at <https://github.com/util-linux/util-linux/issues>.
AVAILABILITY¶
The flock command is part of the util-linux package which can be downloaded from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>.
2024-07-04 | util-linux 2.40.2 |