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