Category

Linux Command


Usage

flock [options] file|directory command [arguments]


Manual

This utility manages flock locks from within shell scripts or from the command line. It can be used in the following forms:

  1. flock [options] file|directory command [arguments]
  2. flock [options] file|directory -c command
  3. flock [options] number
    

The first and second of the above forms wrap the lock around the execution of a command. 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. 

Options

  • -c, --command command: Pass a single command, without arguments, to the shell with -c.
  • -E, --conflict-exit-code number: The exit status used when the -n option is in use, and the conflicting lock exists, or the -w option is in use, and the timeout is reached. The default value is 1. The number has to be in the range of 0 to 255.
  • -F, --no-fork: Do not fork before executing command. Upon execution, the flock process is replaced by command which continues to hold the lock. This option is incompatible with --close as there would otherwise be nothing left to hold the lock.
  • -e, -x, --exclusive: Obtain an exclusive lock, sometimes called a write lock. This is the default.
  • -n, --nb, --nonblock: Fail rather than wait if the lock cannot be immediately acquired. See the -E option for the exit status used.
  • -o, --close: Close the file descriptor on which the lock is held before executing command. This is useful if the command spawns a child process which should not be holding the lock.
  • -s, --shared: Obtain a shared lock, sometimes called a read lock.
  • -u, --unlock: Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock.
  • -w, --wait, --timeout seconds: Fail if the lock cannot be acquired within seconds. Decimal fractional values are allowed. See the -E option for the exit status used. The zero number of seconds is interpreted as --nonblock.
  • --verbose: Report how long it took to acquire the lock, or why the lock could not be obtained.
  • -h, --help: Display help text and exit.
  • -V, --version: Print version and exit.


Share your experience or ask a question