FLOCK(1) General Commands Manual FLOCK(1)

flock
Provide locking API for shell scripts

flock [-dnosvx] [-w timeout] lockfile|lockdir [-c command] | [command ...]

flock [-dnsuvx] [-w timeout] lockfd

The flock utility provides flock(2) access to the command line or scripts. The first form locks a file or directory while the command provided is executed. If the file or directory does not exist, then a file is created.

The second form can use an arbitrary file descriptor that is provided from a shell script for example:

(
	flock -s 100
	# commands to be executed under the lock
) 100> /path/to/lockfile

The following options are available:

command
Pass a command to a the shell.
, --debug
Provide debugging output.
, --nb, - -nonblock
Don't block and fail immediately if the lock could not be obtained.
, --close
Close the file before executing the command. This is useful if the child forks and should not be holding the lock.
, --shared
Obtain a shared lock.
, --unlock
Unlock an existing lock. This is available only for a file descriptor.
, --verbose
On error print an explanation of the failure.
, --wait, - -timeout seconds
Fail if the lock could not be obtained after seconds.
, --exclusive
Obtain an exclusive lock. This is the default.

The flock utility exits 0 on success, and >0 if an error occurs.

shlock(1), flock(2)

An flock utility appeared in NetBSD 6.1.
August 18, 2014 NetBSD 9.4