1.\" 2.\" Copyright (C) 1998 John D. Polstra. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 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. 12.\" 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 23.\" SUCH DAMAGE. 24.\" 25.Dd June 24, 2025 26.Dt LOCKF 1 27.Os 28.Sh NAME 29.Nm lockf 30.Nd execute a command while holding a file lock 31.Sh SYNOPSIS 32.Nm 33.Op Fl knpsTw 34.Op Fl t Ar seconds 35.Ar file 36.Ar command 37.Op Ar arguments 38.Nm 39.Op Fl s 40.Op Fl t Ar seconds 41.Ar fd 42.Sh DESCRIPTION 43The 44.Nm 45utility acquires an exclusive lock on a 46.Ar file , 47creating it if necessary, 48.Bf Em 49and removing the file on exit unless explicitly told not to. 50.Ef 51While holding the lock, it executes a 52.Ar command 53with optional 54.Ar arguments . 55After the 56.Ar command 57completes, 58.Nm 59releases the lock, and removes the 60.Ar file 61unless the 62.Fl k 63option is specified. 64.Bx Ns -style 65locking is used, as described in 66.Xr flock 2 ; 67the mere existence of the 68.Ar file 69is not considered to constitute a lock. 70.Pp 71.Nm 72may also be used to operate on a file descriptor instead of a file. 73If no 74.Ar command 75is supplied, then 76.Ar fd 77must be a file descriptor. 78The version with a 79.Ar command 80may also be used with a file descriptor by supplying it as a path 81.Pa /dev/fd/N , 82where N is the desired file descriptor. 83The 84.Fl k 85option is implied when a file descriptor is in use, and the 86.Fl n 87and 88.Fl w 89options are silently ignored. 90This can be used to lock inside a shell script. 91.Pp 92If the 93.Nm 94utility is being used to facilitate concurrency between a number 95of processes, it is recommended that the 96.Fl k 97option be used. 98This will guarantee lock ordering, as well as implement 99a performance enhanced algorithm which minimizes CPU load associated 100with concurrent unlink, drop and re-acquire activity. 101It should be noted 102that if the 103.Fl k 104option is not used, then no guarantees around lock ordering can be made. 105.Pp 106The following options are supported: 107.Bl -tag -width ".Fl t Ar seconds" 108.It Fl k 109Causes the lock file to be kept (not removed) after the command 110completes. 111.It Fl s 112Causes 113.Nm 114to operate silently. 115Failure to acquire the lock is indicated only in the exit status. 116.It Fl n 117Causes 118.Nm 119to fail if the specified lock 120.Ar file 121does not exist. 122If 123.Fl n 124is not specified, 125.Nm 126will create 127.Ar file 128if necessary. 129.It Fl p 130Write the pid of the 131.Ar command 132to 133.Ar file . 134This option will cause 135.Nm 136to open 137.Ar file 138for writing rather than reading. 139.It Fl T 140Upon receipt of a 141.Dv SIGTERM , 142forward a 143.Dv SIGTERM 144along to the 145.Ar command 146before cleaning up the 147.Ar file 148and exiting. 149By default, 150.Nm 151effectively orphans the 152.Ar command 153after cleaning up the 154.Ar file . 155.It Fl t Ar seconds 156Specifies a timeout for waiting for the lock. 157By default, 158.Nm 159waits indefinitely to acquire the lock. 160If a timeout is specified with this option, 161.Nm 162will wait at most the given number of 163.Ar seconds 164before giving up. 165A timeout of 0 may be given, in which case 166.Nm 167will fail unless it can acquire the lock immediately. 168When a lock times out, 169.Ar command 170is 171.Em not 172executed. 173.It Fl w 174Causes 175.Nm 176to open 177.Ar file 178for writing rather than reading. 179This is necessary on filesystems (including NFSv4) where a file which 180has been opened read-only cannot be exclusively locked. 181.El 182.Pp 183In no event will 184.Nm 185break a lock that is held by another process. 186.Sh EXIT STATUS 187If 188.Nm 189successfully acquires the lock, it returns the exit status produced by 190.Ar command . 191Otherwise, it returns one of the exit codes defined in 192.Xr sysexits 3 , 193as follows: 194.Bl -tag -width ".Dv EX_CANTCREAT" 195.It Dv EX_TEMPFAIL 196The specified lock file was already locked by another process. 197.It Dv EX_CANTCREAT 198The 199.Nm 200utility 201was unable to create the lock file, e.g., because of insufficient access 202privileges. 203.It Dv EX_UNAVAILABLE 204The 205.Fl n 206option is specified and the specified lock file does not exist. 207.It Dv EX_USAGE 208There was an error on the 209.Nm 210command line. 211.It Dv EX_OSERR 212A system call (e.g., 213.Xr fork 2 ) 214failed unexpectedly. 215.It Dv EX_SOFTWARE 216The 217.Ar command 218did not exit normally, 219but may have been signaled or stopped. 220.El 221.Sh EXAMPLES 222The first job takes a lock and sleeps for 5 seconds in the background. 223The second job tries to get the lock and timeouts after 1 second (PID numbers 224will differ): 225.Bd -literal -offset indent 226$ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success" 227[1] 94410 228lockf: mylock: already locked 229.Ed 230.Pp 231The first job takes a lock and sleeps for 1 second in the background. 232The second job waits up to 5 seconds to take the lock and echoes the message on 233success (PID numbers will differ): 234.Bd -literal -offset indent 235$ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success" 236[1] 19995 237Success 238[1]+ Done lockf mylock sleep 1 239.Ed 240Lock a file and run a script, return immediately if the lock is not 241available. Do not delete the file afterward so lock order is 242guaranteed. 243.Pp 244.Dl $ lockf -t 0 -k /tmp/my.lock myscript 245.Pp 246Protect a section of a shell script with a lock, wait up to 5 seconds 247for it to become available. 248Note that the shell script has opened the lock file 249.Fa /tmp/my.lock , 250and 251.Nm 252is performing the lock call exclusively via the passed in file descriptor (9). 253In this case 254.Fl k 255is implied, and 256.Fl w 257has no effect because the file has already been opened by the shell. 258This example assumes that 259.Ql > 260is implemented in the shell by opening and truncating 261.Pa /tmp/my.lock , 262rather than by replacing the lock file. 263.Bd -literal -offset indent 264( 265 lockf -s -t 5 9 266 if [ $? -ne 0 ]; then 267 echo "Failed to obtain lock" 268 exit 1 269 fi 270 271 echo Start 272 # Do some stuff 273 echo End 274) 9>/tmp/my.lock 275.Ed 276.Sh SEE ALSO 277.Xr flock 2 , 278.Xr lockf 3 , 279.Xr sysexits 3 280.Sh HISTORY 281A 282.Nm 283utility first appeared in 284.Fx 2.2 . 285.Sh AUTHORS 286.An John Polstra Aq Mt jdp@polstra.com 287