OSDN Git Service

util-linux 2.37.3: Reserve to translate.
[linuxjm/jm.git] / manual / util-linux / original / man1 / flock.1
1 '\" t
2 .\"     Title: flock
3 .\"    Author: [see the "AUTHOR(S)" section]
4 .\" Generator: Asciidoctor 2.0.15
5 .\"      Date: 2022-01-24
6 .\"    Manual: User Commands
7 .\"    Source: util-linux 2.37.3
8 .\"  Language: English
9 .\"
10 .TH "FLOCK" "1" "2022-01-24" "util\-linux 2.37.3" "User Commands"
11 .ie \n(.g .ds Aq \(aq
12 .el       .ds Aq '
13 .ss \n[.ss] 0
14 .nh
15 .ad l
16 .de URL
17 \fI\\$2\fP <\\$1>\\$3
18 ..
19 .als MTO URL
20 .if \n[.g] \{\
21 .  mso www.tmac
22 .  am URL
23 .    ad l
24 .  .
25 .  am MTO
26 .    ad l
27 .  .
28 .  LINKSTYLE blue R < >
29 .\}
30 .SH "NAME"
31 flock \- manage locks from shell scripts
32 .SH "SYNOPSIS"
33 .sp
34 \fBflock\fP [options] \fIfile\fP|\fIdirectory\fP \fIcommand\fP [\fIarguments\fP]
35 .sp
36 \fBflock\fP [options] \fIfile\fP|\fIdirectory\fP \fB\-c\fP \fIcommand\fP
37 .sp
38 \fBflock\fP [options] \fInumber\fP
39 .SH "DESCRIPTION"
40 .sp
41 This utility manages \fBflock\fP(2) locks from within shell scripts or from the command line.
42 .sp
43 The first and second of the above forms wrap the lock around the execution of a \fIcommand\fP, in a manner similar to \fBsu\fP(1) or \fBnewgrp\fP(1). They lock a specified \fIfile\fP or \fIdirectory\fP, which is created (assuming appropriate permissions) if it does not already exist. By default, if the lock cannot be immediately acquired, \fBflock\fP waits until the lock is available.
44 .sp
45 The third form uses an open file by its file descriptor \fInumber\fP. See the examples below for how that can be used.
46 .SH "OPTIONS"
47 .sp
48 \fB\-c\fP, \fB\-\-command\fP \fIcommand\fP
49 .RS 4
50 Pass a single \fIcommand\fP, without arguments, to the shell with \fB\-c\fP.
51 .RE
52 .sp
53 \fB\-E\fP, \fB\-\-conflict\-exit\-code\fP \fInumber\fP
54 .RS 4
55 The exit status used when the \fB\-n\fP option is in use, and the conflicting lock exists, or the \fB\-w\fP option is in use, and the timeout is reached. The default value is \fB1\fP. The \fInumber\fP has to be in the range of 0 to 255.
56 .RE
57 .sp
58 \fB\-F\fP, \fB\-\-no\-fork\fP
59 .RS 4
60 Do not fork before executing \fIcommand\fP. Upon execution the flock process is replaced by \fIcommand\fP which continues to hold the lock. This option is incompatible with \fB\-\-close\fP as there would otherwise be nothing left to hold the lock.
61 .RE
62 .sp
63 \fB\-e\fP, \fB\-x\fP, \fB\-\-exclusive\fP
64 .RS 4
65 Obtain an exclusive lock, sometimes called a write lock. This is the default.
66 .RE
67 .sp
68 \fB\-n\fP, \fB\-\-nb\fP, \fB\-\-nonblock\fP
69 .RS 4
70 Fail rather than wait if the lock cannot be immediately acquired. See the \fB\-E\fP option for the exit status used.
71 .RE
72 .sp
73 \fB\-o\fP, \fB\-\-close\fP
74 .RS 4
75 Close the file descriptor on which the lock is held before executing \fIcommand\fP. This is useful if \fIcommand\fP spawns a child process which should not be holding the lock.
76 .RE
77 .sp
78 \fB\-s\fP, \fB\-\-shared\fP
79 .RS 4
80 Obtain a shared lock, sometimes called a read lock.
81 .RE
82 .sp
83 \fB\-u\fP, \fB\-\-unlock\fP
84 .RS 4
85 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.
86 .RE
87 .sp
88 \fB\-w\fP, \fB\-\-wait\fP, \fB\-\-timeout\fP \fIseconds\fP
89 .RS 4
90 Fail if the lock cannot be acquired within \fIseconds\fP. Decimal fractional values are allowed. See the \fB\-E\fP option for the exit status used. The zero number of \fIseconds\fP is interpreted as \fB\-\-nonblock\fP.
91 .RE
92 .sp
93 \fB\-\-verbose\fP
94 .RS 4
95 Report how long it took to acquire the lock, or why the lock could not be obtained.
96 .RE
97 .sp
98 \fB\-V\fP, \fB\-\-version\fP
99 .RS 4
100 Display version information and exit.
101 .RE
102 .sp
103 \fB\-h\fP, \fB\-\-help\fP
104 .RS 4
105 Display help text and exit.
106 .RE
107 .SH "EXIT STATUS"
108 .sp
109 The command uses <sysexits.h> exit status values for everything, except when using either of the options \fB\-n\fP or \fB\-w\fP which report a failure to acquire the lock with an exit status given by the \fB\-E\fP option, or 1 by default. The exit status given by \fB\-E\fP has to be in the range of 0 to 255.
110 .sp
111 When using the \fIcommand\fP variant, and executing the child worked, then the exit status is that of the child command.
112 .SH "EXAMPLES"
113 .sp
114 Note that "shell> " in examples is a command line prompt.
115 .sp
116 shell1> flock /tmp \-c cat; shell2> flock \-w .007 /tmp \-c echo; /bin/echo $?
117 .RS 4
118 Set exclusive lock to directory /tmp and the second command will fail.
119 .RE
120 .sp
121 shell1> flock \-s /tmp \-c cat; shell2> flock \-s \-w .007 /tmp \-c echo; /bin/echo $?
122 .RS 4
123 Set shared lock to directory /tmp and the second command will not fail. Notice that attempting to get exclusive lock with second command would fail.
124 .RE
125 .sp
126 shell> flock \-x local\-lock\-file echo \(aqa b c\(aq
127 .RS 4
128 Grab the exclusive lock "local\-lock\-file" before running echo with \(aqa b c\(aq.
129 .RE
130 .sp
131 (; flock \-n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile
132 .RS 4
133 The form is convenient inside shell scripts. The mode used to open the file doesn\(cqt matter to \fBflock\fP; using \fI>\fP or \fI>>\fP allows the lockfile to be created if it does not already exist, however, write permission is required. Using \fI<\fP requires that the file already exists but only read permission is required.
134 .sp
135 [ ${FLOCKER} != $0 ] && exec env FLOCKER="$0 flock \-en $0 $0 $@ ||
136 .RS 4
137 This is useful boilerplate code for shell scripts. Put it at the top of the shell script you want to lock and it\(cqll automatically lock itself on the first run. If the env var \fB$FLOCKER\fP is not set to the shell script that is being run, then execute \fBflock\fP 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 env var to the right value so it doesn\(cqt run again.
138 .RE
139 .RE
140 .sp
141 shell> exec 4<>/var/lock/mylockfile; shell> flock \-n 4
142 .RS 4
143 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.
144 .RE
145 .SH "AUTHORS"
146 .sp
147 .MTO "hpa\(atzytor.com" "H. Peter Anvin" ""
148 .SH "COPYRIGHT"
149 .sp
150 Copyright (co 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.
151 .SH "SEE ALSO"
152 .sp
153 \fBflock\fP(2)
154 .SH "REPORTING BUGS"
155 .sp
156 For bug reports, use the issue tracker at \c
157 .URL "https://github.com/karelzak/util\-linux/issues" "" "."
158 .SH "AVAILABILITY"
159 .sp
160 The \fBflock\fP command is part of the util\-linux package which can be downloaded from \c
161 .URL "https://www.kernel.org/pub/linux/utils/util\-linux/" "Linux Kernel Archive" "."