18931e1d5SRuslan Ermilov.\" 22f278eacSJohn Polstra.\" Copyright (C) 1998 John D. Polstra. All rights reserved. 3c8929a49SJohn Polstra.\" 4c8929a49SJohn Polstra.\" Redistribution and use in source and binary forms, with or without 5c8929a49SJohn Polstra.\" modification, are permitted provided that the following conditions 6c8929a49SJohn Polstra.\" are met: 7c8929a49SJohn Polstra.\" 1. Redistributions of source code must retain the above copyright 8c8929a49SJohn Polstra.\" notice, this list of conditions and the following disclaimer. 9c8929a49SJohn Polstra.\" 2. Redistributions in binary form must reproduce the above copyright 10c8929a49SJohn Polstra.\" notice, this list of conditions and the following disclaimer in the 11c8929a49SJohn Polstra.\" documentation and/or other materials provided with the distribution. 12c8929a49SJohn Polstra.\" 13c8929a49SJohn Polstra.\" THIS SOFTWARE IS PROVIDED BY JOHN D. POLSTRA AND CONTRIBUTORS ``AS IS'' AND 14c8929a49SJohn Polstra.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15c8929a49SJohn Polstra.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16c8929a49SJohn Polstra.\" ARE DISCLAIMED. IN NO EVENT SHALL JOHN D. POLSTRA OR CONTRIBUTORS BE LIABLE 17c8929a49SJohn Polstra.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18c8929a49SJohn Polstra.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19c8929a49SJohn Polstra.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20c8929a49SJohn Polstra.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21c8929a49SJohn Polstra.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22c8929a49SJohn Polstra.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23c8929a49SJohn Polstra.\" SUCH DAMAGE. 24c8929a49SJohn Polstra.\" 25df268d4bSKyle Evans.Dd June 24, 2025 26c8929a49SJohn Polstra.Dt LOCKF 1 27aa12cea2SUlrich Spörlein.Os 28c8929a49SJohn Polstra.Sh NAME 29c8929a49SJohn Polstra.Nm lockf 30c8929a49SJohn Polstra.Nd execute a command while holding a file lock 31c8929a49SJohn Polstra.Sh SYNOPSIS 32c8929a49SJohn Polstra.Nm 33*679f6194SKyle Evans.Op Fl knpsTw 34c8929a49SJohn Polstra.Op Fl t Ar seconds 35c8929a49SJohn Polstra.Ar file 36c8929a49SJohn Polstra.Ar command 37c8929a49SJohn Polstra.Op Ar arguments 3809a7fe0aSKyle Evans.Nm 3909a7fe0aSKyle Evans.Op Fl s 4009a7fe0aSKyle Evans.Op Fl t Ar seconds 4109a7fe0aSKyle Evans.Ar fd 42c8929a49SJohn Polstra.Sh DESCRIPTION 43c8929a49SJohn PolstraThe 44c8929a49SJohn Polstra.Nm 45c8929a49SJohn Polstrautility acquires an exclusive lock on a 46c8929a49SJohn Polstra.Ar file , 47f2839e75SEivind Eklundcreating it if necessary, 4839c59153SRuslan Ermilov.Bf Em 496fb9b618SGiorgos Keramidasand removing the file on exit unless explicitly told not to. 5039c59153SRuslan Ermilov.Ef 51c8929a49SJohn PolstraWhile holding the lock, it executes a 52c8929a49SJohn Polstra.Ar command 53c8929a49SJohn Polstrawith optional 54c8929a49SJohn Polstra.Ar arguments . 55c8929a49SJohn PolstraAfter the 56c8929a49SJohn Polstra.Ar command 57c8929a49SJohn Polstracompletes, 58c8929a49SJohn Polstra.Nm 592f278eacSJohn Polstrareleases the lock, and removes the 602f278eacSJohn Polstra.Ar file 612f278eacSJohn Polstraunless the 622f278eacSJohn Polstra.Fl k 63753d686dSRuslan Ermilovoption is specified. 64753d686dSRuslan Ermilov.Bx Ns -style 65753d686dSRuslan Ermilovlocking is used, as described in 66c8929a49SJohn Polstra.Xr flock 2 ; 67c8929a49SJohn Polstrathe mere existence of the 68c8929a49SJohn Polstra.Ar file 69c8929a49SJohn Polstrais not considered to constitute a lock. 70c8929a49SJohn Polstra.Pp 7109a7fe0aSKyle Evans.Nm 7209a7fe0aSKyle Evansmay also be used to operate on a file descriptor instead of a file. 7309a7fe0aSKyle EvansIf no 7409a7fe0aSKyle Evans.Ar command 7509a7fe0aSKyle Evansis supplied, then 7609a7fe0aSKyle Evans.Ar fd 7709a7fe0aSKyle Evansmust be a file descriptor. 7809a7fe0aSKyle EvansThe version with a 7909a7fe0aSKyle Evans.Ar command 8009a7fe0aSKyle Evansmay also be used with a file descriptor by supplying it as a path 8109a7fe0aSKyle Evans.Pa /dev/fd/N , 8209a7fe0aSKyle Evanswhere N is the desired file descriptor. 8309a7fe0aSKyle EvansThe 8409a7fe0aSKyle Evans.Fl k 8509a7fe0aSKyle Evansoption is implied when a file descriptor is in use, and the 8609a7fe0aSKyle Evans.Fl n 8709a7fe0aSKyle Evansand 8809a7fe0aSKyle Evans.Fl w 8909a7fe0aSKyle Evansoptions are silently ignored. 9009a7fe0aSKyle EvansThis can be used to lock inside a shell script. 9109a7fe0aSKyle Evans.Pp 926a53f0a5SChristian S.J. PeronIf the 936a53f0a5SChristian S.J. Peron.Nm 946a53f0a5SChristian S.J. Peronutility is being used to facilitate concurrency between a number 956a53f0a5SChristian S.J. Peronof processes, it is recommended that the 966a53f0a5SChristian S.J. Peron.Fl k 97de8f7dcbSRuslan Ermilovoption be used. 98de8f7dcbSRuslan ErmilovThis will guarantee lock ordering, as well as implement 996a53f0a5SChristian S.J. Perona performance enhanced algorithm which minimizes CPU load associated 100de8f7dcbSRuslan Ermilovwith concurrent unlink, drop and re-acquire activity. 101de8f7dcbSRuslan ErmilovIt should be noted 1026a53f0a5SChristian S.J. Peronthat if the 1036a53f0a5SChristian S.J. Peron.Fl k 1046a53f0a5SChristian S.J. Peronoption is not used, then no guarantees around lock ordering can be made. 1056a53f0a5SChristian S.J. Peron.Pp 106c8929a49SJohn PolstraThe following options are supported: 107c8c35875SRuslan Ermilov.Bl -tag -width ".Fl t Ar seconds" 1082f278eacSJohn Polstra.It Fl k 1092f278eacSJohn PolstraCauses the lock file to be kept (not removed) after the command 1102f278eacSJohn Polstracompletes. 111c8929a49SJohn Polstra.It Fl s 112c8929a49SJohn PolstraCauses 113c8929a49SJohn Polstra.Nm 114c8929a49SJohn Polstrato operate silently. 115c8929a49SJohn PolstraFailure to acquire the lock is indicated only in the exit status. 116b5be420aSEitan Adler.It Fl n 117b5be420aSEitan AdlerCauses 118b5be420aSEitan Adler.Nm 119b5be420aSEitan Adlerto fail if the specified lock 120b5be420aSEitan Adler.Ar file 12115597d5dSFernando Apesteguíadoes not exist. 12215597d5dSFernando ApesteguíaIf 123b5be420aSEitan Adler.Fl n 124b5be420aSEitan Adleris not specified, 125b5be420aSEitan Adler.Nm 126b5be420aSEitan Adlerwill create 127b5be420aSEitan Adler.Ar file 128b5be420aSEitan Adlerif necessary. 129df268d4bSKyle Evans.It Fl p 130df268d4bSKyle EvansWrite the pid of the 131df268d4bSKyle Evans.Ar command 132df268d4bSKyle Evansto 133df268d4bSKyle Evans.Ar file . 134df268d4bSKyle EvansThis option will cause 135df268d4bSKyle Evans.Nm 136df268d4bSKyle Evansto open 137df268d4bSKyle Evans.Ar file 138df268d4bSKyle Evansfor writing rather than reading. 139*679f6194SKyle Evans.It Fl T 140*679f6194SKyle EvansUpon receipt of a 141*679f6194SKyle Evans.Dv SIGTERM , 142*679f6194SKyle Evansforward a 143*679f6194SKyle Evans.Dv SIGTERM 144*679f6194SKyle Evansalong to the 145*679f6194SKyle Evans.Ar command 146*679f6194SKyle Evansbefore cleaning up the 147*679f6194SKyle Evans.Ar file 148*679f6194SKyle Evansand exiting. 149*679f6194SKyle EvansBy default, 150*679f6194SKyle Evans.Nm 151*679f6194SKyle Evanseffectively orphans the 152*679f6194SKyle Evans.Ar command 153*679f6194SKyle Evansafter cleaning up the 154*679f6194SKyle Evans.Ar file . 155c8929a49SJohn Polstra.It Fl t Ar seconds 1566a3e8b0aSRuslan ErmilovSpecifies a timeout for waiting for the lock. 1576a3e8b0aSRuslan ErmilovBy default, 158c8929a49SJohn Polstra.Nm 159c8929a49SJohn Polstrawaits indefinitely to acquire the lock. 160c8929a49SJohn PolstraIf a timeout is specified with this option, 161c8929a49SJohn Polstra.Nm 162c8929a49SJohn Polstrawill wait at most the given number of 163c8929a49SJohn Polstra.Ar seconds 1646a3e8b0aSRuslan Ermilovbefore giving up. 1656a3e8b0aSRuslan ErmilovA timeout of 0 may be given, in which case 166c8929a49SJohn Polstra.Nm 167c8929a49SJohn Polstrawill fail unless it can acquire the lock immediately. 168f2839e75SEivind EklundWhen a lock times out, 169f2839e75SEivind Eklund.Ar command 170f2839e75SEivind Eklundis 171f2839e75SEivind Eklund.Em not 172f2839e75SEivind Eklundexecuted. 173437bab48SColin Percival.It Fl w 174437bab48SColin PercivalCauses 175437bab48SColin Percival.Nm 176437bab48SColin Percivalto open 177437bab48SColin Percival.Ar file 178437bab48SColin Percivalfor writing rather than reading. 179437bab48SColin PercivalThis is necessary on filesystems (including NFSv4) where a file which 180437bab48SColin Percivalhas been opened read-only cannot be exclusively locked. 181c8929a49SJohn Polstra.El 182c8929a49SJohn Polstra.Pp 183c8929a49SJohn PolstraIn no event will 184c8929a49SJohn Polstra.Nm 185c8929a49SJohn Polstrabreak a lock that is held by another process. 186a866e170SRuslan Ermilov.Sh EXIT STATUS 187c8929a49SJohn PolstraIf 188c8929a49SJohn Polstra.Nm 189ae06cb4dSJohn Polstrasuccessfully acquires the lock, it returns the exit status produced by 190c8929a49SJohn Polstra.Ar command . 191ae06cb4dSJohn PolstraOtherwise, it returns one of the exit codes defined in 192ae06cb4dSJohn Polstra.Xr sysexits 3 , 193ae06cb4dSJohn Polstraas follows: 194c8c35875SRuslan Ermilov.Bl -tag -width ".Dv EX_CANTCREAT" 195ae06cb4dSJohn Polstra.It Dv EX_TEMPFAIL 196ae06cb4dSJohn PolstraThe specified lock file was already locked by another process. 197ae06cb4dSJohn Polstra.It Dv EX_CANTCREAT 198e8937ba0SPhilippe CharnierThe 199ae06cb4dSJohn Polstra.Nm 200e8937ba0SPhilippe Charnierutility 201ae06cb4dSJohn Polstrawas unable to create the lock file, e.g., because of insufficient access 202ae06cb4dSJohn Polstraprivileges. 203b5be420aSEitan Adler.It Dv EX_UNAVAILABLE 204b5be420aSEitan AdlerThe 205b5be420aSEitan Adler.Fl n 206b5be420aSEitan Adleroption is specified and the specified lock file does not exist. 207ae06cb4dSJohn Polstra.It Dv EX_USAGE 208ae06cb4dSJohn PolstraThere was an error on the 209ae06cb4dSJohn Polstra.Nm 210ae06cb4dSJohn Polstracommand line. 211ae06cb4dSJohn Polstra.It Dv EX_OSERR 212c8c35875SRuslan ErmilovA system call (e.g., 213c8c35875SRuslan Ermilov.Xr fork 2 ) 214c8c35875SRuslan Ermilovfailed unexpectedly. 2157f15c32dSDavid Malone.It Dv EX_SOFTWARE 2167f15c32dSDavid MaloneThe 2177f15c32dSDavid Malone.Ar command 2187f15c32dSDavid Malonedid not exit normally, 2197f15c32dSDavid Malonebut may have been signaled or stopped. 220ae06cb4dSJohn Polstra.El 22115597d5dSFernando Apesteguía.Sh EXAMPLES 22215597d5dSFernando ApesteguíaThe first job takes a lock and sleeps for 5 seconds in the background. 22315597d5dSFernando ApesteguíaThe second job tries to get the lock and timeouts after 1 second (PID numbers 22415597d5dSFernando Apesteguíawill differ): 22515597d5dSFernando Apesteguía.Bd -literal -offset indent 22615597d5dSFernando Apesteguía$ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success" 22715597d5dSFernando Apesteguía[1] 94410 22815597d5dSFernando Apesteguíalockf: mylock: already locked 22915597d5dSFernando Apesteguía.Ed 23015597d5dSFernando Apesteguía.Pp 23115597d5dSFernando ApesteguíaThe first job takes a lock and sleeps for 1 second in the background. 23215597d5dSFernando ApesteguíaThe second job waits up to 5 seconds to take the lock and echoes the message on 23315597d5dSFernando Apesteguíasuccess (PID numbers will differ): 23415597d5dSFernando Apesteguía.Bd -literal -offset indent 23515597d5dSFernando Apesteguía$ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success" 23615597d5dSFernando Apesteguía[1] 19995 23715597d5dSFernando ApesteguíaSuccess 23815597d5dSFernando Apesteguía[1]+ Done lockf mylock sleep 1 23915597d5dSFernando Apesteguía.Ed 24009a7fe0aSKyle EvansLock a file and run a script, return immediately if the lock is not 24109a7fe0aSKyle Evansavailable. Do not delete the file afterward so lock order is 24209a7fe0aSKyle Evansguaranteed. 24309a7fe0aSKyle Evans.Pp 24409a7fe0aSKyle Evans.Dl $ lockf -t 0 -k /tmp/my.lock myscript 24509a7fe0aSKyle Evans.Pp 24609a7fe0aSKyle EvansProtect a section of a shell script with a lock, wait up to 5 seconds 24709a7fe0aSKyle Evansfor it to become available. 24809a7fe0aSKyle EvansNote that the shell script has opened the lock file 24909a7fe0aSKyle Evans.Fa /tmp/my.lock , 25009a7fe0aSKyle Evansand 25109a7fe0aSKyle Evans.Nm 25209a7fe0aSKyle Evansis performing the lock call exclusively via the passed in file descriptor (9). 25309a7fe0aSKyle EvansIn this case 25409a7fe0aSKyle Evans.Fl k 25509a7fe0aSKyle Evansis implied, and 25609a7fe0aSKyle Evans.Fl w 25709a7fe0aSKyle Evanshas no effect because the file has already been opened by the shell. 25809a7fe0aSKyle EvansThis example assumes that 25909a7fe0aSKyle Evans.Ql > 26009a7fe0aSKyle Evansis implemented in the shell by opening and truncating 26109a7fe0aSKyle Evans.Pa /tmp/my.lock , 26209a7fe0aSKyle Evansrather than by replacing the lock file. 26309a7fe0aSKyle Evans.Bd -literal -offset indent 26409a7fe0aSKyle Evans( 26509a7fe0aSKyle Evans lockf -s -t 5 9 26609a7fe0aSKyle Evans if [ $? -ne 0 ]; then 26709a7fe0aSKyle Evans echo "Failed to obtain lock" 26809a7fe0aSKyle Evans exit 1 26909a7fe0aSKyle Evans fi 27009a7fe0aSKyle Evans 27109a7fe0aSKyle Evans echo Start 27209a7fe0aSKyle Evans # Do some stuff 27309a7fe0aSKyle Evans echo End 27409a7fe0aSKyle Evans) 9>/tmp/my.lock 27509a7fe0aSKyle Evans.Ed 276c8929a49SJohn Polstra.Sh SEE ALSO 277ae06cb4dSJohn Polstra.Xr flock 2 , 278bef6922bSMark Johnston.Xr lockf 3 , 279306005e7SPhilippe Charnier.Xr sysexits 3 2809c8efe99SDavid E. O'Brien.Sh HISTORY 281e8937ba0SPhilippe CharnierA 2829c8efe99SDavid E. O'Brien.Nm 283e8937ba0SPhilippe Charnierutility first appeared in 2849c8efe99SDavid E. O'Brien.Fx 2.2 . 285c8929a49SJohn Polstra.Sh AUTHORS 2862b7af31cSBaptiste Daroussin.An John Polstra Aq Mt jdp@polstra.com 287