]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/flock.1
*: spelling and grammar fixes
[thirdparty/util-linux.git] / sys-utils / flock.1
CommitLineData
baf39af1 1.\" -----------------------------------------------------------------------
e9da7722 2.\"
a120aaa7 3.\" Copyright 2003-2006 H. Peter Anvin - All Rights Reserved
baf39af1
KZ
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:
e9da7722 13.\"
baf39af1
KZ
14.\" The above copyright notice and this permission notice shall
15.\" be included in all copies or substantial portions of the Software.
e9da7722 16.\"
baf39af1
KZ
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.\" -----------------------------------------------------------------------
e33ee284 27.TH FLOCK 1 "July 2014" "util-linux" "User Commands"
d162fcb5 28.SH NAME
232dc924 29flock \- manage locks from shell scripts
d162fcb5 30.SH SYNOPSIS
e9da7722 31.B flock
e33ee284
BS
32[options]
33.IR file | "directory command " [ arguments ]
e9da7722
SK
34.br
35.B flock
e33ee284
BS
36[options]
37.IR file | directory
38.BI \-c " command"
e9da7722
SK
39.br
40.B flock
e33ee284 41.RI [options] " number"
d162fcb5 42.SH DESCRIPTION
baf39af1
KZ
43This utility manages
44.BR flock (2)
e33ee284 45locks from within shell scripts or from the command line.
d162fcb5 46.PP
e33ee284
BS
47The first and second of the above forms wrap the lock around the execution of a
48.IR command ,
49in a manner similar to
baf39af1
KZ
50.BR su (1)
51or
52.BR newgrp (1).
e33ee284
BS
53They lock a specified \fIfile\fR or \fIdirectory\fR, which is created (assuming
54appropriate permissions) if it does not already exist. By default, if the
e9da7722
SK
55lock cannot be immediately acquired,
56.B flock
baf39af1 57waits until the lock is available.
e9da7722 58.PP
e33ee284
BS
59The third form uses an open file by its file descriptor \fInumber\fR.
60See the examples below for how that can be used.
baf39af1
KZ
61.SH OPTIONS
62.TP
5e43af7e
BS
63.BR \-c , " \-\-command " \fIcommand
64Pass a single \fIcommand\fR, without arguments, to the shell with
65.BR \-c .
baf39af1 66.TP
b06c1ca6 67.BR \-E , " \-\-conflict\-exit\-code " \fInumber
a87f49f6 68The exit status used when the \fB\-n\fP option is in use, and the
5e43af7e
BS
69conflicting lock exists, or the \fB\-w\fP option is in use,
70and the timeout is reached. The default value is \fB1\fR.
e5781b3d 71The \fInumber\fR has to be in the range of 0 to 255.
5e43af7e 72.TP
70325140
TB
73.BR \-F , " \-\-no\-fork"
74Do not fork before executing
75.IR command .
76Upon execution the flock process is replaced by
0bb7e904 77.I command
70325140
TB
78which 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
5e43af7e 81.BR \-e , " \-x" , " \-\-exclusive"
baf39af1
KZ
82Obtain an exclusive lock, sometimes called a write lock. This is the
83default.
84.TP
5e43af7e 85.BR \-n , " \-\-nb" , " \-\-nonblock"
827b1cee 86Fail rather than wait if the lock cannot be
baf39af1 87immediately acquired.
827b1cee 88See the
e33ee284 89.B \-E
a87f49f6 90option for the exit status used.
baf39af1 91.TP
5e43af7e 92.BR \-o , " \-\-close"
baf39af1 93Close the file descriptor on which the lock is held before executing
e33ee284 94.IR command .
e9da7722 95This is useful if
e33ee284 96.I command
e9da7722 97spawns a child process which should not be holding the lock.
baf39af1 98.TP
5e43af7e
BS
99.BR \-s , " \-\-shared"
100Obtain a shared lock, sometimes called a read lock.
827b1cee 101.TP
5e43af7e
BS
102.BR \-u , " \-\-unlock"
103Drop a lock. This is usually not required, since a lock is automatically
104dropped when the file is closed. However, it may be required in special
105cases, for example if the enclosed command group may have forked a background
106process which should not be holding the lock.
a120aaa7 107.TP
5e43af7e
BS
108.BR \-w , " \-\-wait" , " \-\-timeout " \fIseconds
109Fail if the lock cannot be acquired within
110.IR seconds .
111Decimal fractional values are allowed.
112See the
113.B \-E
a87f49f6 114option for the exit status used. The zero number of
0bb7e904 115.I seconds
c4604c38 116is interpreted as \fB\-\-nonblock\fR.
5e43af7e 117.TP
59dc9f28
SK
118.B \-\-verbose
119Report how long it took to acquire the lock, or why the lock could not be
120obtained.
121.TP
5e43af7e 122.BR \-V , " \-\-version"
b4362b6f 123Display version information and exit.
5e43af7e
BS
124.TP
125.BR \-h , " \-\-help"
126Display help text and exit.
67e63c12
MK
127.SH EXIT STATUS
128The command uses
129.B sysexits.h
130exit status values for everything, except when using either of the options
131.B \-n
132or
133.B \-w
3c560686 134which report a failure to acquire the lock with an exit status given by the
67e63c12 135.B \-E
e5781b3d
KZ
136option, or 1 by default. The exit status given by
137.B \-E has to be in the range of 0 to 255.
67e63c12
MK
138.PP
139When using the \fIcommand\fR variant, and executing the child worked, then
140the exit status is that of the child command.
3bc92f31 141.SH EXAMPLES
5db212c0 142Note that "shell> " in examples is a command line prompt.
e9da7722 143.TP
1c4c6024 144shell1> flock /tmp \-c cat
e9da7722 145.TQ
1c4c6024 146shell2> flock \-w .007 /tmp \-c echo; /bin/echo $?
e9da7722
SK
147Set exclusive lock to directory /tmp and the second command will fail.
148.TP
1c4c6024 149shell1> flock \-s /tmp \-c cat
e9da7722 150.TQ
1c4c6024 151shell2> flock \-s \-w .007 /tmp \-c echo; /bin/echo $?
e9da7722
SK
152Set shared lock to directory /tmp and the second command will not fail.
153Notice that attempting to get exclusive lock with second command would fail.
154.TP
1c4c6024 155shell> flock \-x local-lock-file echo 'a b c'
295dd902
MF
156Grab the exclusive lock "local-lock-file" before running echo with 'a b c'.
157.TP
e9da7722
SK
158(
159.TQ
1c4c6024 160 flock \-n 9 || exit 1
e9da7722
SK
161.TQ
162 # ... commands executed under lock ...
163.TQ
164) 9>/var/lock/mylockfile
165The form is convenient inside shell scripts. The mode used to open the file
166doesn't matter to
167.BR flock ;
168using
169.I >
170or
171.I >>
172allows the lockfile to be created if it does not already exist, however,
173write permission is required. Using
174.I <
175requires that the file already exists but only read permission is required.
295dd902 176.TP
1c4c6024 177[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock \-en "$0" "$0" "$@" || :
295dd902
MF
178This is useful boilerplate code for shell scripts. Put it at the top of the
179shell script you want to lock and it'll automatically lock itself on the first
180run. If the env var $FLOCKER is not set to the shell script that is being run,
181then execute flock and grab an exclusive non-blocking lock (using the script
182itself as the lock file) before re-execing itself with the right arguments. It
183also sets the FLOCKER env var to the right value so it doesn't run again.
966bcac3 184.TP
5db212c0 185shell> exec 4<>/var/lock/mylockfile
966bcac3 186.TQ
5db212c0 187shell> flock -n 4
966bcac3
J
188This form is convenient for locking a file without spawning a subprocess.
189The shell opens the lock file for reading and writing as file descriptor 4,
190then flock is used to lock the descriptor.
a8d0d330 191.SH AUTHORS
e9da7722
SK
192.UR hpa@zytor.com
193H. Peter Anvin
194.UE
baf39af1 195.SH COPYRIGHT
a120aaa7 196Copyright \(co 2003\-2006 H. Peter Anvin.
baf39af1
KZ
197.br
198This is free software; see the source for copying conditions. There is NO
199warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5a829806 200.SH SEE ALSO
baf39af1 201.BR flock (2)
86d62711 202.SH AVAILABILITY
601d12fb 203The flock command is part of the util-linux package and is available from
d673b74e 204.UR https://\:www.kernel.org\:/pub\:/linux\:/utils\:/util-linux/
e9da7722
SK
205Linux Kernel Archive
206.UE .