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       -V, --version
79           Display version information and exit.
80
81       -h, --help
82           Display help text 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

EXAMPLES

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

AUTHORS

136       H. Peter Anvin <hpa@zytor.com>
137
139       Copyright © 2003-2006 H. Peter Anvin. This is free software; see the
140       source for copying conditions. There is NO warranty; not even for
141       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
142

SEE ALSO

144       flock(2)
145

REPORTING BUGS

147       For bug reports, use the issue tracker at
148       https://github.com/karelzak/util-linux/issues.
149

AVAILABILITY

151       The flock command is part of the util-linux package which can be
152       downloaded from Linux Kernel Archive
153       <https://www.kernel.org/pub/linux/utils/util-linux/>.
154
155
156
157util-linux 2.37.2                 2021-06-02                          FLOCK(1)
Impressum