Linux command
flock 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Example
flock /tmp/lock.lock command
Example
flock -n /tmp/lock.lock command
Example
flock -w 10 /tmp/lock.lock command
Example
flock -n -E 42 /tmp/lock.lock command
Example
flock /tmp/lock.lock -c "command1 && command2"
Example
flock -s /tmp/lock.lock command
说明
flock manages file locks from shell scripts. It executes a command while holding a lock on a specified file or directory, creating the lock file if needed. By default, the command waits indefinitely for the lock to become available. This is useful for ensuring only one instance of a command runs at a time.
参数
- -c, --command
- Execute a single command string via the shell
- -e, -x, --exclusive
- Obtain an exclusive (write) lock (default)
- -s, --shared
- Obtain a shared (read) lock
- -n, --nb, --nonblock
- Fail immediately if the lock is not available
- -w, --wait, --timeout SECONDS
- Fail if the lock is not acquired within specified seconds
- -o, --close
- Close the file descriptor before executing the command
- -u, --unlock
- Manually drop the lock
- -E, --conflict-exit-code NUM
- Exit code when lock fails with -n or timeout (default: 1)
- -F, --no-fork
- Do not fork before executing the command
- --verbose
- Report how long the lock took to acquire
FAQ
What is the flock command used for?
flock manages file locks from shell scripts. It executes a command while holding a lock on a specified file or directory, creating the lock file if needed. By default, the command waits indefinitely for the lock to become available. This is useful for ensuring only one instance of a command runs at a time.
How do I run a basic flock example?
Run `flock /tmp/lock.lock command` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -c, --command do in flock?
Execute a single command string via the shell