1FLOCK(1)                        H. Peter Anvin                        FLOCK(1)
2
3
4

NAME

6       flock - Manage locks from shell scripts
7

SYNOPSIS

9       flock [-sxon] [-w timeout] lockfile [-c] command...
10
11       flock [-sxon] [-w timeout] lockdir [-c] command...
12
13       flock [-sxun] [-w timeout] fd
14

DESCRIPTION

16       This  utility  manages  flock(2) locks from within shell scripts or the
17       command line.
18
19       The first and second forms wraps the lock around the executing  a  com‐
20       mand,  in a manner similar to su(1) or newgrp(1).  It locks a specified
21       file or directory, which is created (assuming appropriate permissions),
22       if it does not already exist.
23
24       The  third form is convenient inside shell scripts, and is usually used
25       the following manner:
26
27       (
28         flock -s 200
29         # ... commands executed under lock ...
30       ) 200>/var/lock/mylockfile
31
32       The mode used to open the file doesn't matter to flock; using >  or  >>
33       allows  the  lockfile  to be created if it does not already exist, how‐
34       ever, write permission is required; using  <  requires  that  the  file
35       already exists but only read permission is required.
36
37       By  default,  if  the  lock cannot be immediately acquired, flock waits
38       until the lock is available.
39

OPTIONS

41       -s, --shared
42              Obtain a shared lock, sometimes called a read lock.
43
44       -x, -e, --exclusive
45              Obtain an exclusive lock, sometimes called a write  lock.   This
46              is the default.
47
48       -u, --unlock
49              Drop  a  lock.   This  is  usually not required, since a lock is
50              automatically dropped when the file is closed.  However, it  may
51              be  required  in special cases, for example if the enclosed com‐
52              mand group may have forked a background process which should not
53              be holding the lock.
54
55       -n, --nb, --nonblock
56              Fail  (with an exit code of 1) rather than wait if the lock can‐
57              not be immediately acquired.
58
59       -w, --wait, --timeout seconds
60              Fail (with an exit code of 1) if the  lock  cannot  be  acquired
61              within seconds seconds.  Decimal fractional values are allowed.
62
63       -o, --close
64              Close  the file descriptor on which the lock is held before exe‐
65              cuting command.  This  is  useful  if  command  spawns  a  child
66              process which should not be hold ing the lock.
67
68       -c, --command command
69              Pass a single command to the shell with -c.
70
71       -h, --help
72              Print a help message.
73

AUTHOR

75       Written by H. Peter Anvin <hpa@zytor.com>.
76
78       Copyright © 2003-2006 H. Peter Anvin.
79       This is free software; see the source for copying conditions.  There is
80       NO warranty; not even for MERCHANTABILITY or FITNESS FOR  A  PARTICULAR
81       PURPOSE.
82

SEE ALSO

84       flock(2)
85

AVAILABILITY

87       The flock command is part of the util-linux-ng package and is available
88       from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
89
90
91
92flock utility                     4 Feb 2006                          FLOCK(1)
Impressum