]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/flock.1
docs: fix some things that were overlooked during the first pass
[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 .PP
44 This utility manages
45 .BR flock (2)
46 locks from within shell scripts or from the command line.
47 .PP
48 The first and second of the above forms wrap the lock around the execution of a
49 .IR command ,
50 in a manner similar to
51 .BR su (1)
52 or
53 .BR newgrp (1).
54 They lock a specified \fIfile\fR or \fIdirectory\fR, which is created (assuming
55 appropriate permissions) if it does not already exist. By default, if the
56 lock cannot be immediately acquired,
57 .B flock
58 waits until the lock is available.
59 .PP
60 The third form uses an open file by its file descriptor \fInumber\fR.
61 See the examples below for how that can be used.
62 .SH OPTIONS
63 .TP
64 .BR \-c , " \-\-command " \fIcommand
65 Pass a single \fIcommand\fR, without arguments, to the shell with
66 .BR \-c .
67 .TP
68 .BR \-E , " \-\-conflict-exit-code " \fInumber
69 The exit code used when the \fB\-n\fP option is in use, and the
70 conflicting lock exists, or the \fB\-w\fP option is in use,
71 and the timeout is reached. The default value is \fB1\fR.
72 .TP
73 .BR \-e , " \-x" , " \-\-exclusive"
74 Obtain an exclusive lock, sometimes called a write lock. This is the
75 default.
76 .TP
77 .BR \-n , " \-\-nb" , " \-\-nonblock"
78 Fail rather than wait if the lock cannot be
79 immediately acquired.
80 See the
81 .B \-E
82 option for the exit code used.
83 .TP
84 .BR \-o , " \-\-close"
85 Close the file descriptor on which the lock is held before executing
86 .IR command .
87 This is useful if
88 .I command
89 spawns a child process which should not be holding the lock.
90 .TP
91 .BR \-s , " \-\-shared"
92 Obtain a shared lock, sometimes called a read lock.
93 .TP
94 .BR \-u , " \-\-unlock"
95 Drop a lock. This is usually not required, since a lock is automatically
96 dropped when the file is closed. However, it may be required in special
97 cases, for example if the enclosed command group may have forked a background
98 process which should not be holding the lock.
99 .TP
100 .BR \-w , " \-\-wait" , " \-\-timeout " \fIseconds
101 Fail if the lock cannot be acquired within
102 .IR seconds .
103 Decimal fractional values are allowed.
104 See the
105 .B \-E
106 option for the exit code used.
107 .TP
108 .BR \-V , " \-\-version"
109 Display version information and exit.
110 .TP
111 .BR \-h , " \-\-help"
112 Display help text and exit.
113 .SH EXAMPLES
114 .TP
115 shell1> flock /tmp -c cat
116 .TQ
117 shell2> flock -w .007 /tmp -c echo; /bin/echo $?
118 Set exclusive lock to directory /tmp and the second command will fail.
119 .TP
120 shell1> flock -s /tmp -c cat
121 .TQ
122 shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?
123 Set shared lock to directory /tmp and the second command will not fail.
124 Notice that attempting to get exclusive lock with second command would fail.
125 .TP
126 shell> flock -x local-lock-file echo 'a b c'
127 Grab the exclusive lock "local-lock-file" before running echo with 'a b c'.
128 .TP
129 (
130 .TQ
131 flock -n 9 || exit 1
132 .TQ
133 # ... commands executed under lock ...
134 .TQ
135 ) 9>/var/lock/mylockfile
136 The form is convenient inside shell scripts. The mode used to open the file
137 doesn't matter to
138 .BR flock ;
139 using
140 .I >
141 or
142 .I >>
143 allows the lockfile to be created if it does not already exist, however,
144 write permission is required. Using
145 .I <
146 requires that the file already exists but only read permission is required.
147 .TP
148 [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :
149 This is useful boilerplate code for shell scripts. Put it at the top of the
150 shell script you want to lock and it'll automatically lock itself on the first
151 run. If the env var $FLOCKER is not set to the shell script that is being run,
152 then execute flock and grab an exclusive non-blocking lock (using the script
153 itself as the lock file) before re-execing itself with the right arguments. It
154 also sets the FLOCKER env var to the right value so it doesn't run again.
155 .SH "EXIT STATUS"
156 The command uses
157 .B sysexits.h
158 return values for everything, except when using either of the options
159 .B \-n
160 or
161 .B \-w
162 which report a failure to acquire the lock with a return value given by the
163 .B \-E
164 option, or 1 by default.
165 .PP
166 When using the \fIcommand\fR variant, and executing the child worked, then
167 the exit status is that of the child command.
168 .SH AUTHOR
169 .UR hpa@zytor.com
170 H. Peter Anvin
171 .UE
172 .SH COPYRIGHT
173 Copyright \(co 2003\-2006 H. Peter Anvin.
174 .br
175 This is free software; see the source for copying conditions. There is NO
176 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
177 .SH "SEE ALSO"
178 .BR flock (2)
179 .SH AVAILABILITY
180 The flock command is part of the util-linux package and is available from
181 .UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
182 Linux Kernel Archive
183 .UE .