Section: User Commands (1)
Updated: July 2014
flock [options] file|directory command [arguments]
flock [options] file|directory -c command
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.
flock -n 9 || exit 1
# ... commands executed under lock ... ) 9>/var/lock/mylockfile The form is convenient inside shell scripts. The mode used to open the file doesn't matter to flock; using > or >> allows the lockfile to be created if it does not already exist, however, write permission is required. Using < requires that the file already exists but only read permission is required.:
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.
Copyright © 2003-2006 H. Peter Anvin.
The flock command is part of the util-linux package and is available from Linux Kernel Archive
Tutoriais de Tecnologia Web