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,--commandcommand โ pass a single command, with no arguments, to the shell using-c.-E,--conflict-exit-codenumber โ the exit status used when the-noption is in use and the conflicting lock exists, or when the-woption is in use and the timeout is reached. default is1. the number has to be in the range of0to255. no exceptions bestie.-F,--no-forkโ do not fork before executing command. upon execution, theflockprocess 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-Eoption 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,--timeoutseconds โ fail if the lock can't be acquired within seconds. decimal fractional values are allowed, so yes, you can be that precise. see the-Eoption for the exit status used. and heads up: zero seconds is interpreted as--nonblock.--fcntlโ instead offlock(2), apply anfcntl(2)open file description lock (that is, using theF_OFD_SETLK(non-blocking) orF_OFD_SETLKW(blocking) commands). these locks are independent of the ones applied viaflock(2), but โ unlike traditional POSIXfcntl(2)locks (F_SETLK,F_SETLKW) โ they have semantics matching those offlock(2). โจ
This is only available on kernel versions >= 3.15.
--startoffset โ the starting offset for the lock. this option implies--fcntl.--lengthnumber โ 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 ๐
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 ๐ง
๐ 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