]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/flock.1
9d7a56fe15584eb594d37fd075d1c41f47d66388
[thirdparty/util-linux.git] / sys-utils / flock.1
1 .\" -----------------------------------------------------------------------
2 .\"
3 .\" Copyright 2003-2006 H. Peter Anvin - All Rights Reserved
4 .\"
5 .\" Permission is hereby granted, free of charge, to any person
6 .\" obtaining a copy of this software and associated documentation
7 .\" files (the "Software"), to deal in the Software without
8 .\" restriction, including without limitation the rights to use,
9 .\" copy, modify, merge, publish, distribute, sublicense, and/or
10 .\" sell copies of the Software, and to permit persons to whom
11 .\" the Software is furnished to do so, subject to the following
12 .\" conditions:
13 .\"
14 .\" The above copyright notice and this permission notice shall
15 .\" be included in all copies or substantial portions of the Software.
16 .\"
17 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 .\" OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 .\" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 .\" HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 .\" WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 .\" FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 .\" OTHER DEALINGS IN THE SOFTWARE.
25 .\"
26 .\" -----------------------------------------------------------------------
27 .TH FLOCK 1 "July 2014" "util-linux" "User Commands"
28 .SH NAME
29 flock \- manage locks from shell scripts
30 .SH SYNOPSIS
31 .B flock
32 [options]
33 .IR file | "directory command " [ arguments ]
34 .br
35 .B flock
36 [options]
37 .IR file | directory
38 .BI \-c " command"
39 .br
40 .B flock
41 .RI [options] " number"
42 .SH DESCRIPTION
43 This utility manages
44 .BR flock (2)
45 locks from within shell scripts or from the command line.
46 .PP
47 The first and second of the above forms wrap the lock around the execution of a
48 .IR command ,
49 in a manner similar to
50 .BR su (1)
51 or
52 .BR newgrp (1).
53 They lock a specified \fIfile\fR or \fIdirectory\fR, which is created (assuming
54 appropriate permissions) if it does not already exist. By default, if the
55 lock cannot be immediately acquired,
56 .B flock
57 waits until the lock is available.
58 .PP
59 The third form uses an open file by its file descriptor \fInumber\fR.
60 See the examples below for how that can be used.
61 .SH OPTIONS
62 .TP
63 .BR \-c , " \-\-command " \fIcommand
64 Pass a single \fIcommand\fR, without arguments, to the shell with
65 .BR \-c .
66 .TP
67 .BR \-E , " \-\-conflict\-exit\-code " \fInumber
68 The exit status used when the \fB\-n\fP option is in use, and the
69 conflicting lock exists, or the \fB\-w\fP option is in use,
70 and the timeout is reached. The default value is \fB1\fR.
71 The \fInumber\fR has to be in the range of 0 to 255.
72 .TP
73 .BR \-F , " \-\-no\-fork"
74 Do not fork before executing
75 .IR command .
76 Upon execution the flock process is replaced by
77 .I command
78 which continues to hold the lock. This option is incompatible with
79 \fB\-\-close\fR as there would otherwise be nothing left to hold the lock.
80 .TP
81 .BR \-e , " \-x" , " \-\-exclusive"
82 Obtain an exclusive lock, sometimes called a write lock. This is the
83 default.
84 .TP
85 .BR \-n , " \-\-nb" , " \-\-nonblock"
86 Fail rather than wait if the lock cannot be
87 immediately acquired.
88 See the
89 .B \-E
90 option for the exit status used.
91 .TP
92 .BR \-o , " \-\-close"
93 Close the file descriptor on which the lock is held before executing
94 .IR command .
95 This is useful if
96 .I command
97 spawns a child process which should not be holding the lock.
98 .TP
99 .BR \-s , " \-\-shared"
100 Obtain a shared lock, sometimes called a read lock.
101 .TP
102 .BR \-u , " \-\-unlock"
103 Drop a lock. This is usually not required, since a lock is automatically
104 dropped when the file is closed. However, it may be required in special
105 cases, for example if the enclosed command group may have forked a background
106 process which should not be holding the lock.
107 .TP
108 .BR \-w , " \-\-wait" , " \-\-timeout " \fIseconds
109 Fail if the lock cannot be acquired within
110 .IR seconds .
111 Decimal fractional values are allowed.
112 See the
113 .B \-E
114 option for the exit status used. The zero number of
115 .I seconds
116 is interpreted as \fB\-\-nonblock\fR.
117 .TP
118 .B \-\-verbose
119 Report how long it took to acquire the lock, or why the lock could not be
120 obtained.
121 .TP
122 .BR \-V , " \-\-version"
123 Display version information and exit.
124 .TP
125 .BR \-h , " \-\-help"
126 Display help text and exit.
127 .SH EXIT STATUS
128 The command uses
129 .B sysexits.h
130 exit status values for everything, except when using either of the options
131 .B \-n
132 or
133 .B \-w
134 which report a failure to acquire the lock with an exit status given by the
135 .B \-E
136 option, or 1 by default. The exit status given by
137 .B \-E has to be in the range of 0 to 255.
138 .PP
139 When using the \fIcommand\fR variant, and executing the child worked, then
140 the exit status is that of the child command.
141 .SH EXAMPLES
142 Note that "shell> " in examples is a command line prompt.
143 .TP
144 shell1> flock /tmp \-c cat
145 .TQ
146 shell2> flock \-w .007 /tmp \-c echo; /bin/echo $?
147 Set exclusive lock to directory /tmp and the second command will fail.
148 .TP
149 shell1> flock \-s /tmp \-c cat
150 .TQ
151 shell2> flock \-s \-w .007 /tmp \-c echo; /bin/echo $?
152 Set shared lock to directory /tmp and the second command will not fail.
153 Notice that attempting to get exclusive lock with second command would fail.
154 .TP
155 shell> flock \-x local-lock-file echo 'a b c'
156 Grab the exclusive lock "local-lock-file" before running echo with 'a b c'.
157 .TP
158 (
159 .TQ
160 flock \-n 9 || exit 1
161 .TQ
162 # ... commands executed under lock ...
163 .TQ
164 ) 9>/var/lock/mylockfile
165 The form is convenient inside shell scripts. The mode used to open the file
166 doesn't matter to
167 .BR flock ;
168 using
169 .I >
170 or
171 .I >>
172 allows the lockfile to be created if it does not already exist, however,
173 write permission is required. Using
174 .I <
175 requires that the file already exists but only read permission is required.
176 .TP
177 [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock \-en "$0" "$0" "$@" || :
178 This is useful boilerplate code for shell scripts. Put it at the top of the
179 shell script you want to lock and it'll automatically lock itself on the first
180 run. If the env var $FLOCKER is not set to the shell script that is being run,
181 then execute flock and grab an exclusive non-blocking lock (using the script
182 itself as the lock file) before re-execing itself with the right arguments. It
183 also sets the FLOCKER env var to the right value so it doesn't run again.
184 .TP
185 shell> exec 4<>/var/lock/mylockfile
186 .TQ
187 shell> flock -n 4
188 This form is convenient for locking a file without spawning a subprocess.
189 The shell opens the lock file for reading and writing as file descriptor 4,
190 then flock is used to lock the descriptor.
191 .SH AUTHORS
192 .UR hpa@zytor.com
193 H. Peter Anvin
194 .UE
195 .SH COPYRIGHT
196 Copyright \(co 2003\-2006 H. Peter Anvin.
197 .br
198 This is free software; see the source for copying conditions. There is NO
199 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
200 .SH SEE ALSO
201 .BR flock (2)
202 .SH AVAILABILITY
203 The flock command is part of the util-linux package and is available from
204 .UR https://\:www.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
205 Linux Kernel Archive
206 .UE .