flock [options] file|directory command [arguments] flock [options] file|directory -c command flock [options] number
This utility manages flock(2) locks from within shell scripts or from the command line.
The first and second of the above forms wrap the lock around the execution of a command, in a manner similar to su(1) or newgrp(1). They lock a specified file or directory, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, flock waits until the lock is available.
The third form uses an open file by its file descriptor number. See the examples below for how that can be used.
The command uses sysexits.h return values for everything, except when using either of the options -n or -w which report a failure to acquire the lock with a return value given by the -E option, or 1 by default.
When using the command variant, and executing the child worked, then the exit status is that of the child command.
H. Peter Anvin
Copyright © 2003-2006 H. Peter Anvin. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The flock command is part of the util-linux package and is available from Linux Kernel Archive