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