2 .\" Copyright (C) 1998 John D. Polstra. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .Nd execute a command while holding a file lock
41 utility acquires an exclusive lock on a
43 creating it if necessary,
45 and removing the file on exit unless explicitly told not to.
47 While holding the lock, it executes a
55 releases the lock, and removes the
61 locking is used, as described in
63 the mere existence of the
65 is not considered to constitute a lock.
69 utility is being used to facilitate concurrency between a number
70 of processes, it is recommended that the
73 This will guarantee lock ordering, as well as implement
74 a performance enhanced algorithm which minimizes CPU load associated
75 with concurrent unlink, drop and re-acquire activity.
79 option is not used, then no guarantees around lock ordering can be made.
81 The following options are supported:
82 .Bl -tag -width ".Fl t Ar seconds"
84 Causes the lock file to be kept (not removed) after the command
90 Failure to acquire the lock is indicated only in the exit status.
94 to fail if the specified lock
105 Specifies a timeout for waiting for the lock.
108 waits indefinitely to acquire the lock.
109 If a timeout is specified with this option,
111 will wait at most the given number of
114 A timeout of 0 may be given, in which case
116 will fail unless it can acquire the lock immediately.
117 When a lock times out,
127 for writing rather than reading.
128 This is necessary on filesystems (including NFSv4) where a file which
129 has been opened read-only cannot be exclusively locked.
134 break a lock that is held by another process.
138 successfully acquires the lock, it returns the exit status produced by
140 Otherwise, it returns one of the exit codes defined in
143 .Bl -tag -width ".Dv EX_CANTCREAT"
145 The specified lock file was already locked by another process.
150 was unable to create the lock file, e.g., because of insufficient access
152 .It Dv EX_UNAVAILABLE
155 option is specified and the specified lock file does not exist.
157 There was an error on the
167 did not exit normally,
168 but may have been signaled or stopped.
171 The first job takes a lock and sleeps for 5 seconds in the background.
172 The second job tries to get the lock and timeouts after 1 second (PID numbers
174 .Bd -literal -offset indent
175 $ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success"
177 lockf: mylock: already locked
180 The first job takes a lock and sleeps for 1 second in the background.
181 The second job waits up to 5 seconds to take the lock and echoes the message on
182 success (PID numbers will differ):
183 .Bd -literal -offset indent
184 $ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success"
187 [1]+ Done lockf mylock sleep 1
196 utility first appeared in
199 .An John Polstra Aq Mt jdp@polstra.com