1 .\" $Id: flock.1,v 1.3 2006/02/04 20:41:57 hpa Exp $
2 .\" -----------------------------------------------------------------------
4 .\" Copyright 2003-2006 H. Peter Anvin - All Rights Reserved
6 .\" Permission is hereby granted, free of charge, to any person
7 .\" obtaining a copy of this software and associated documentation
8 .\" files (the "Software"), to deal in the Software without
9 .\" restriction, including without limitation the rights to use,
10 .\" copy, modify, merge, publish, distribute, sublicense, and/or
11 .\" sell copies of the Software, and to permit persons to whom
12 .\" the Software is furnished to do so, subject to the following
15 .\" The above copyright notice and this permission notice shall
16 .\" be included in all copies or substantial portions of the Software.
18 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 .\" OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 .\" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 .\" HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 .\" WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 .\" FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 .\" OTHER DEALINGS IN THE SOFTWARE.
27 .\" -----------------------------------------------------------------------
28 .TH FLOCK "1" "4 Feb 2006" "flock utility" "H. Peter Anvin"
30 flock \- Manage locks from shell scripts
32 \fBflock\fP [\fB\-sxon\fP] [\fB\-w\fP \fItimeout\fP] \fIlockfile\fP [\fB\-c\fP] \fIcommand...\fP
34 \fBflock\fP [\fB\-sxun\fP] [\fB\-w\fP \fItimeout\fP] \fIfd\fP
39 locks from within shell scripts or the command line.
41 The first form wraps the lock around the executing a command, in a manner similar to
45 It locks a specified file, which is created (assuming appropriate
46 permissions), if it does not already exist.
48 The second form is conveninent inside shell scripts, and is usually
49 used the following manner:
55 # ... commands executed under lock ...
57 ) 200>/var/lock/mylockfile\fP
59 The mode used to open the file doesn't matter to \fBflock\fP; using
60 \fC>\fP or \fP>>\fP allows the lockfile to be created if it does not
61 already exist, however, write permission is required; using \fC<\fP
62 requires that the file already exists but only read permission is
65 By default, if the lock cannot be immediately acquired, \fBflock\fP
66 waits until the lock is available.
69 \fB\-s\fP, \fB\-\-shared\fP
70 Obtain a shared lock, sometimes called a read lock.
72 \fB\-x\fP, \fB\-e\fP, \fB\-\-exclusive\fP
73 Obtain an exclusive lock, sometimes called a write lock. This is the
76 \fB\-u\fP, \fB\-\-unlock\fP
77 Drop a lock. This is usually not required, since a lock is
78 automatically dropped when the file is closed. However, it may be
79 required in special cases, for example if the enclosed command group
80 may have forked a background process which should not be holding the
83 \fB\-n\fP, \fB\-\-nb\fP, \fB\-\-nonblock\fP
84 Fail (with an exit code of 1) rather than wait if the lock cannot be
87 \fB\-w\fP, \fB\-\-wait\fP, \fB\-\-timeout\fP \fIseconds\fP
88 Fail (with an exit code of 1) if the lock cannot be acquired within
89 \fIseconds\fP seconds. Decimal fractional values are allowed.
91 \fB\-o\fP, \fB\-\-close\fP
92 Close the file descriptor on which the lock is held before executing
93 \fIcommand\fP. This is useful if \fIcommand\fP spawns a child process
94 which should not be hold ing the lock.
96 \fB\-c\fP, \fB\-\-command\fP \fIcommand\fP
97 Pass a single \fIcommand\fP to the shell with \fB\-c\fP.
99 \fB\-h\fP, \fB\-\-help\fP
100 Print a help message.
102 Written by H. Peter Anvin <hpa@zytor.com>.
104 Copyright \(co 2003\-2006 H. Peter Anvin.
106 This is free software; see the source for copying conditions. There is NO
107 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.