flock

From RaySoft

This utility manages flock locks from within shell scripts or the command line.[1]

WARNING:
flock is not available for all UNIX derivates. It is missing for example on macOS.

Documentation

Syntax

flock [PARAMETER ...] DIR|FILE [-c] COMMAND
flock [PARAMETER ...] FD

Parameters

-c COMMAND, --command COMMAND
Pass a single command to the shell.
-e, -x, --exclusive
Obtain an exclusive lock, sometimes called a write lock. This is the default.
-n, --nb, --nonblack
Fail (with an exit code of 1) rather than wait if the lock cannot be immediately acquired.

Examples

Do not run the script deploy-macos.sh if it is already running
flock --nonblack '/tmp/test.lock' --command '/home/alex/dev/deploy-macos.sh'

References

  1. man 1 'flock'