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