FLOCK(1)

โœจ FLOCK(1) โœจ

okay bestie, gather round, because flock โ€” a User Commands girly from the util-linux universe โ€” is about to become your whole personality ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

flock โ€” manage locks from shell scripts

likeโ€ฆ it's giving "I do locking, and I do it from your shell scripts." iconic behavior fr fr.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

flock [options] file|directory command [arguments]

flock [options] file|directory -c command

flock [options] number

three whole forms, three whole vibes, all valid, all slaying ๐Ÿ‘‘


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So this little utility manages flock(2) locks from inside shell scripts, or straight up from the command line. no cap.

The first and second forms above wrap the lock around the execution of a command, kinda like how su(1) or newgrp(1) do it. They lock a specified file or directory, and if that thing doesn't exist yet, it gets created (assuming you have the right permissions, obviously). By default? If the lock can't be grabbed immediately, flock justโ€ฆ waits. patiently. like a bestie holding your spot in line ๐Ÿซถ

The third form uses an open file by its file descriptor number. Peep the examples below to see how that one gets used ๐Ÿ‘€


๐Ÿ’– OPTIONS ๐Ÿ’–

  • -c, --command command โ€” pass a single command, with no arguments, to the shell using -c.
  • -E, --conflict-exit-code number โ€” the exit status used when the -n option is in use and the conflicting lock exists, or when the -w option is in use and the timeout is reached. default is 1. the number has to be in the range of 0 to 255. no exceptions bestie.
  • -F, --no-fork โ€” do not fork before executing command. upon execution, the flock process gets replaced by command, which then continues to hold the lock. โš ๏ธ this option is incompatible with --close, because otherwise there'd be nothing left to hold the lock. like, at all. nothing. gone.
  • -e, -x, --exclusive โ€” obtain an exclusive lock, sometimes called a write lock. this is the default, so you're already doing it, main character ๐Ÿซถ
  • -n, --nb, --nonblocking โ€” fail rather than wait if the lock can't be immediately acquired. see the -E option for which exit status gets used.
  • -o, --close โ€” close the file descriptor the lock is held on before executing command. super useful if command spawns a child process that shouldn't be holding the lock. rude of them tbh.
  • -s, --shared โ€” obtain a shared lock, sometimes called a read lock.
  • -u, --unlock โ€” drop a lock. usually you don't need this, because a lock gets automatically dropped when the file is closed. but 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. messy of them ๐Ÿ’€
  • -w, --wait, --timeout seconds โ€” fail if the lock can't be acquired within seconds. decimal fractional values are allowed, so yes, you can be that precise. see the -E option for the exit status used. and heads up: zero seconds is interpreted as --nonblock.
  • --fcntl โ€” instead of flock(2), apply an fcntl(2) open file description lock (that is, using the F_OFD_SETLK (non-blocking) or F_OFD_SETLKW (blocking) commands). these locks are independent of the ones applied via flock(2), but โ€” unlike traditional POSIX fcntl(2) locks (F_SETLK, F_SETLKW) โ€” they have semantics matching those of flock(2). โœจ
This is only available on kernel versions >= 3.15.
  • --start offset โ€” the starting offset for the lock. this option implies --fcntl.
  • --length number โ€” the number of bytes to lock. this option implies --fcntl.
  • --verbose โ€” report how long it took to acquire the lock, or why the lock could not be obtained. the gossip we all deserve ๐Ÿ“
  • -h, --help โ€” display help text and exit.
  • -V, --version โ€” display version and exit.

โญ EXIT STATUS โญ

The command uses <sysexits.h> exit status values for everything, except when you're using either -n or -w โ€” those report a failure to acquire the lock with an exit status given by the -E option, or 1 by default. and the exit status given by -E has to be in the range of 0 to 255.

When using the command variant, and executing the child worked, then the exit status is that of the child command. the child is the main character, apparently ๐Ÿ‘‘


๐Ÿ”ฅ NOTES ๐Ÿ”ฅ

flock does not detect deadlock. see flock(2) for details. so likeโ€ฆ be careful, bestie ๐Ÿ˜ญ

Some file systems (e. g. NFS and CIFS) have a limited implementation of flock(2), and flock may always fail. for details see flock(2), nfs(5) and mount.cifs(8). depending on mount options, flock can always fail there. ๐Ÿ’€


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

Note that "shell> " in the examples is a command line prompt.


Set exclusive lock to directory /tmp and the second command will fail.

shell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo; /bin/echo $?

Set shared lock to directory /tmp and the second command will not fail. Notice that attempting to get an exclusive lock with the second command would fail.

shell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?

Grab the exclusive lock "local-lock-file" before running echo with 'a b c'.

shell> flock -x local-lock-file echo 'a b c'

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.

(; flock -n 9 || exit 1; # ... commands executed under lock ...; )
9>/var/lock/mylockfile

This is useful boilerplate code for shell scripts. Put it at the top of the shell script you want to lock, and it'll automatically lock itself on the first run. If the environment variable $FLOCKER is not set to the shell script that is being run, then execute flock and grab an exclusive non-blocking lock (using the script itself as the lock file) before re-execing itself with the right arguments. It also sets the FLOCKER environment variable to the right value so it doesn't run again. self-locking queen behavior ๐Ÿ’…

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

This form is convenient for locking a file without spawning a subprocess. The shell opens the lock file for reading and writing as file descriptor 4, then flock is used to lock the descriptor.

shell> exec 4<>/var/lock/mylockfile; shell> flock -n 4

๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

H. Peter Anvin <hpa@zytor.com>


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.


๐Ÿงš SEE ALSO ๐Ÿงš

flock(2), fcntl(2)


๐Ÿ’œ REPORTING BUGS ๐Ÿ’œ

For bug reports, use the issue tracker <https://github.com/util-linux/util-linux/issues>.


๐Ÿฆ„ AVAILABILITY ๐Ÿฆ„

The flock command is part of the util-linux package which can be downloaded from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>.


and that's the whole thing, bestie โ€” this page came to you from util-linux 2.42.4, dated 2026-09-21, and it's the FLOCK(1) manual, mwah ๐Ÿ’– now go lock something (responsibly) โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: util-linux 2.42.4-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND ISC AND LGPL-2.1-or-later AND LicenseRef-PublicDomain
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms