1.\" $NetBSD: flock.1,v 1.13 2019/10/04 16:14:05 uwe Exp $ 2.\" 3.\" Copyright (c) 2012 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Christos Zoulas. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.\" 31.Dd October 4, 2019 32.Dt FLOCK 1 33.Os 34.Sh NAME 35.Nm flock 36.Nd Provide locking API for shell scripts 37.Sh SYNOPSIS 38.\" 39.Nm 40.Op Fl dnosvx 41.Op Fl w Ar timeout 42.Ar file Ns \^|\^ Ns Ar directory 43.Ar command 44.Op Ar args No ... 45.\" 46.Nm 47.Op Fl dnosvx 48.Op Fl w Ar timeout 49.Ar file Ns \^|\^ Ns Ar directory 50.Fl c Ar command 51.\" 52.Nm 53.Op Fl dnsuvx 54.Op Fl w Ar timeout 55.Ar number 56.Sh DESCRIPTION 57The 58.Nm 59utility provides 60.Xr flock 2 61access to the command line or scripts. 62The first two forms lock the specified 63.Ar file 64or 65.Ar directory 66while the provided command is executed. 67If the path does not exist, then a file of that name is created with 68mode 0600. 69.Pp 70The third form can use an arbitrary file descriptor 71.Ar number 72that is provided from a shell script for example: 73.Bd -literal -offset indent 74( 75 flock -s 100 76 # commands to be executed under the lock 77) 100> /path/to/lockfile 78.Ed 79.Pp 80The following options are available: 81.Bl -tag -width Fl 82.It Fl c Ar command 83Pass a command to a the shell. 84.It Fl d , Fl Fl debug 85Provide debugging output. 86.It Fl n , Fl Fl nb , Fl Fl nonblock 87Don't block and fail immediately if the lock could not be obtained. 88.It Fl o , Fl Fl close 89Close the file before executing the command. 90This is useful if the child forks and should not be holding the lock. 91.It Fl s , Fl Fl shared 92Obtain a shared lock. 93.It Fl u , Fl Fl unlock 94Unlock an existing lock. 95This is available only for a file descriptor. 96.It Fl v , Fl Fl verbose 97On error print an explanation of the failure. 98.It Fl w , Fl Fl wait , Fl Fl timeout Ar seconds 99Fail if the lock could not be obtained after 100.Ar seconds . 101.It Fl x , Fl Fl exclusive 102Obtain an exclusive lock. 103This is the default. 104.El 105.Sh EXIT STATUS 106.Ex -std 107.Sh SEE ALSO 108.Xr shlock 1 , 109.Xr flock 2 110.Sh HISTORY 111An 112.Nm 113utility appeared in 114.Nx 6.1 . 115