1FLOCK(1)                         User Commands                        FLOCK(1)
2
3
4

NAME

6       flock - manage locks from shell scripts
7

SYNOPSIS

9       flock [options] file|directory command [arguments]
10
11       flock [options] file|directory -c command
12
13       flock [options] number
14

DESCRIPTION

16       This utility manages flock(2) locks from within shell scripts or from
17       the command line.
18
19       The first and second of the above forms wrap the lock around the
20       execution of a command, in a manner similar to su(1) or newgrp(1). They
21       lock a specified file or directory, which is created (assuming
22       appropriate permissions) if it does not already exist. By default, if
23       the lock cannot be immediately acquired, flock waits until the lock is
24       available.
25
26       The third form uses an open file by its file descriptor number. See the
27       examples below for how that can be used.
28

OPTIONS

30       -c, --command command
31           Pass a single command, without arguments, to the shell with -c.
32
33       -E, --conflict-exit-code number
34           The exit status used when the -n option is in use, and the
35           conflicting lock exists, or the -w option is in use, and the
36           timeout is reached. The default value is 1. The number has to be in
37           the range of 0 to 255.
38
39       -F, --no-fork
40           Do not fork before executing command. Upon execution the flock
41           process is replaced by command which continues to hold the lock.
42           This option is incompatible with --close as there would otherwise
43           be nothing left to hold the lock.
44
45       -e, -x, --exclusive
46           Obtain an exclusive lock, sometimes called a write lock. This is
47           the default.
48
49       -n, --nb, --nonblock
50           Fail rather than wait if the lock cannot be immediately acquired.
51           See the -E option for the exit status used.
52
53       -o, --close
54           Close the file descriptor on which the lock is held before
55           executing command. This is useful if command spawns a child process
56           which should not be holding the lock.
57
58       -s, --shared
59           Obtain a shared lock, sometimes called a read lock.
60
61       -u, --unlock
62           Drop a lock. This is usually not required, since a lock is
63           automatically dropped when the file is closed. However, it may be
64           required in special cases, for example if the enclosed command
65           group may have forked a background process which should not be
66           holding the lock.
67
68       -w, --wait, --timeout seconds
69           Fail if the lock cannot be acquired within seconds. Decimal
70           fractional values are allowed. See the -E option for the exit
71           status used. The zero number of seconds is interpreted as
72           --nonblock.
73
74       --verbose
75           Report how long it took to acquire the lock, or why the lock could
76           not be obtained.
77
78       -h, --help
79           Display help text and exit.
80
81       -V, --version
82           Print version and exit.
83

EXIT STATUS

85       The command uses <sysexits.h> exit status values for everything, except
86       when using either of the options -n or -w which report a failure to
87       acquire the lock with an exit status given by the -E option, or 1 by
88       default. The exit status given by -E has to be in the range of 0 to
89       255.
90
91       When using the command variant, and executing the child worked, then
92       the exit status is that of the child command.
93

NOTES

95       flock does not detect deadlock. See flock(2) for details.
96
97       Some file systems (e. g. NFS and CIFS) have a limited implementation of
98       flock(2) and flock may always fail. For details see flock(2), nfs(5)
99       and mount.cifs(8). Depending on mount options, flock can always fail
100       there.
101

EXAMPLES

103       Note that "shell> " in examples is a command line prompt.
104
105       shell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo;
106       /bin/echo $?
107           Set exclusive lock to directory /tmp and the second command will
108           fail.
109
110       shell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo;
111       /bin/echo $?
112           Set shared lock to directory /tmp and the second command will not
113           fail. Notice that attempting to get exclusive lock with second
114           command would fail.
115
116       shell> flock -x local-lock-file echo 'a b c'
117           Grab the exclusive lock "local-lock-file" before running echo with
118           'a b c'.
119
120       (; flock -n 9 || exit 1; # ... commands executed under lock ...; )
121       9>/var/lock/mylockfile
122           The form is convenient inside shell scripts. The mode used to open
123           the file doesn’t matter to flock; using > or >> allows the lockfile
124           to be created if it does not already exist, however, write
125           permission is required. Using < requires that the file already
126           exists but only read permission is required.
127
128       [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0"
129       "$@" || :
130           This is useful boilerplate code for shell scripts. Put it at the
131           top of the shell script you want to lock and it’ll automatically
132           lock itself on the first run. If the environment variable $FLOCKER
133           is not set to the shell script that is being run, then execute
134           flock and grab an exclusive non-blocking lock (using the script
135           itself as the lock file) before re-execing itself with the right
136           arguments. It also sets the FLOCKER environment variable to the
137           right value so it doesn’t run again.
138
139       shell> exec 4<>/var/lock/mylockfile; shell> flock -n 4
140           This form is convenient for locking a file without spawning a
141           subprocess. The shell opens the lock file for reading and writing
142           as file descriptor 4, then flock is used to lock the descriptor.
143

AUTHORS

145       H. Peter Anvin <hpa@zytor.com>
146
148       Copyright © 2003-2006 H. Peter Anvin. This is free software; see the
149       source for copying conditions. There is NO warranty; not even for
150       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
151

SEE ALSO

153       flock(2)
154

REPORTING BUGS

156       For bug reports, use the issue tracker at
157       https://github.com/util-linux/util-linux/issues.
158

AVAILABILITY

160       The flock command is part of the util-linux package which can be
161       downloaded from Linux Kernel Archive
162       <https://www.kernel.org/pub/linux/utils/util-linux/>.
163
164
165
166util-linux 2.39.2                 2023-06-14                          FLOCK(1)
Impressum