| Main index | Section 1 | 日本語 | Options |
lockf
may also be used to operate on a file descriptor instead of a file.
If no
command
is supplied, then
fd
must be a file descriptor.
The version with a
command
may also be used with a file descriptor by supplying it as a path
/dev/fd/N,
where N is the desired file descriptor.
The
If the
lockf
utility is being used to facilitate concurrency between a number
of processes, it is recommended that the
The following options are supported:
| | Causes the lock file to be kept (not removed) after the command completes. |
| | Causes lockf to operate silently. Failure to acquire the lock is indicated only in the exit status. |
| |
Causes
lockf
to fail if the specified lock
file
does not exist.
If
|
| | |
| Specifies a timeout for waiting for the lock. By default, lockf waits indefinitely to acquire the lock. If a timeout is specified with this option, lockf will wait at most the given number of seconds before giving up. A timeout of 0 may be given, in which case lockf will fail unless it can acquire the lock immediately. When a lock times out, command is not executed. | |
| | Causes lockf to open file for writing rather than reading. This is necessary on filesystems (including NFSv4) where a file which has been opened read-only cannot be exclusively locked. |
In no event will lockf break a lock that is held by another process.
| EX_TEMPFAIL | The specified lock file was already locked by another process. |
| EX_CANTCREAT | |
| The lockf utility was unable to create the lock file, e.g., because of insufficient access privileges. | |
| EX_UNAVAILABLE | |
|
The
| |
| EX_USAGE | There was an error on the lockf command line. |
| EX_OSERR | A system call (e.g., fork(2)) failed unexpectedly. |
| EX_SOFTWARE | The command did not exit normally, but may have been signaled or stopped. |
$ lockf mylock sleep 5 & lockf -t 1 mylock echo "Success" [1] 94410 lockf: mylock: already locked
The first job takes a lock and sleeps for 1 second in the background. The second job waits up to 5 seconds to take the lock and echoes the message on success (PID numbers will differ):
$ lockf mylock sleep 1 & lockf -t 5 mylock echo "Success" [1] 19995 Success [1]+ Done lockf mylock sleep 1Lock a file and run a script, return immediately if the lock is not available. Do not delete the file afterward so lock order is guaranteed.
$ lockf -t 0 -k /tmp/my.lock myscript
Protect a section of a shell script with a lock, wait up to 5 seconds
for it to become available.
Note that the shell script has opened the lock file
/tmp/my.lock,
and
lockf
is performing the lock call exclusively via the passed in file descriptor (9).
In this case
( lockf -s -t 5 9 if [ $? -ne 0 ]; then echo "Failed to obtain lock" exit 1 fiecho Start # Do some stuff echo End ) 9>/tmp/my.lock
| LOCKF (1) | November 25, 2023 |
| Main index | Section 1 | 日本語 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
| “ | A computer would deserve to be called intelligent if it could deceive a human into believing that it was human. | ” |
| — Alan Turing | ||