1FLOCK(1) H. Peter Anvin FLOCK(1)
2
3
4
6 flock - Manage locks from shell scripts
7
9 flock [-sxon] [-w timeout] lockfile [-c] command...
10
11 flock [-sxun] [-w timeout] fd
12
14 This utility manages flock(2) locks from within shell scripts or the
15 command line.
16
17 The first form wraps the lock around the executing a command, in a man‐
18 ner similar to su(1) or newgrp(1). It locks a specified file, which is
19 created (assuming appropriate permissions), if it does not already
20 exist.
21
22 The second form is conveninent inside shell scripts, and is usually
23 used the following manner:
24
25 (
26 flock -s 200
27 # ... commands executed under lock ...
28 ) 200>/var/lock/mylockfile
29
30 The mode used to open the file doesn't matter to flock; using > or >>
31 allows the lockfile to be created if it does not already exist, how‐
32 ever, write permission is required; using < requires that the file
33 already exists but only read permission is required.
34
35 By default, if the lock cannot be immediately acquired, flock waits
36 until the lock is available.
37
39 -s, --shared
40 Obtain a shared lock, sometimes called a read lock.
41
42 -x, -e, --exclusive
43 Obtain an exclusive lock, sometimes called a write lock. This
44 is the default.
45
46 -u, --unlock
47 Drop a lock. This is usually not required, since a lock is
48 automatically dropped when the file is closed. However, it may
49 be required in special cases, for example if the enclosed com‐
50 mand group may have forked a background process which should not
51 be holding the lock.
52
53 -n, --nb, --nonblock
54 Fail (with an exit code of 1) rather than wait if the lock can‐
55 not be immediately acquired.
56
57 -w, --wait, --timeout seconds
58 Fail (with an exit code of 1) if the lock cannot be acquired
59 within seconds seconds. Decimal fractional values are allowed.
60
61 -o, --close
62 Close the file descriptor on which the lock is held before exe‐
63 cuting command. This is useful if command spawns a child
64 process which should not be hold ing the lock.
65
66 -c, --command command
67 Pass a single command to the shell with -c.
68
69 -h, --help
70 Print a help message.
71
73 Written by H. Peter Anvin <hpa@zytor.com>.
74
76 Copyright © 2003-2006 H. Peter Anvin.
77 This is free software; see the source for copying conditions. There is
78 NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
79 PURPOSE.
80
82 flock(2)
83
84
85
86flock utility 4 Feb 2006 FLOCK(1)