From b6472a3f08463ea887bf61d1b7c58c5620b92387 Mon Sep 17 00:00:00 2001 From: p0358 Date: Wed, 30 Jul 2025 06:11:06 +0200 Subject: [PATCH] flock: improvements and fix of misleading wording (#17373) Co-authored-by: Managor <42655600+Managor@users.noreply.github.com> Co-authored-by: Sebastiaan Speck <12570668+sebastiaanspeck@users.noreply.github.com> --- pages/linux/flock.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pages/linux/flock.md b/pages/linux/flock.md index 1f00325227..8ae2346818 100644 --- a/pages/linux/flock.md +++ b/pages/linux/flock.md @@ -1,17 +1,25 @@ # flock -> Manage locks from shell scripts. -> It can be used to ensure that only one process of a command is running. +> Manage file locks from shell scripts. +> It can be used to ensure that only one instance of a command is running. > More information: . -- Run a command with a file lock as soon as the lock is not required by others: +- Run a command with a file lock as soon as the lock is available: -`flock {{path/to/lock.lock}} {{[-c|--command]}} "{{command}}"` +`flock {{path/to/lock.lock}} {{command}}` -- Run a command with a file lock, and exit if the lock doesn't exist: +- Run a command with a file lock, or exit if the lock is currently being held (with exit code 1): -`flock {{path/to/lock.lock}} {{[-n|--nonblock]}} {{[-c|--command]}} "{{command}}"` +`flock {{path/to/lock.lock}} {{[-n|--nonblock]}} {{command}}` -- Run a command with a file lock, and exit with a specific error code if the lock doesn't exist: +- Run a command with a file lock, or exit with a specific error code if the lock is currently being held: -`flock {{path/to/lock.lock}} {{[-n|--nonblock]}} {{[-E|--conflict-exit-code]}} {{error_code}} {{[-c|--command]}} "{{command}}"` +`flock {{path/to/lock.lock}} {{[-n|--nonblock]}} {{[-E|--conflict-exit-code]}} {{123}} {{command}}` + +- Run a command with a file lock, waiting up to 10 seconds for the lock to be available before giving up: + +`flock {{path/to/lock.lock}} {{[-w|--timeout]}} 10 {{command}}` + +- Backup a bunch of files, waiting for the previous `tar` command to finish if it's still running elsewhere and holding the same lock file (can be used in a `cron` job that runs often): + +`flock {{path/to/backup.lock}} {{tar -cvf path/to/backup.tar path/to/data/}}`