Flock: Difference between revisions
(Created page with "Flock is a tool to prevent running a script multiple times. ==Usage== ===CLI=== ===In a bash script=== <syntaxhighlight lang="bash"> exec {lock_fd}>$LOCKFILE || exit 1 flock -n "$lock_fd" || { echo "Another instance is running" >&2 exit 1 } # Note that the fd will unlock automatically at the end of the script. # Unlock once all background processes are done. exec {lock_fd}>&- # Or unlock immediately flock -u "$lock_fd" </syntaxhighlight>") |
(→CLI) |
||
Line 3: | Line 3: | ||
==Usage== | ==Usage== | ||
===CLI=== | ===CLI=== | ||
<pre> | |||
flock -n $LOCKFILE $MY_COMMAND | |||
</pre> | |||
===In a bash script=== | ===In a bash script=== | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> |
Latest revision as of 19:27, 29 January 2022
Flock is a tool to prevent running a script multiple times.
Usage
CLI
flock -n $LOCKFILE $MY_COMMAND
In a bash script
exec {lock_fd}>$LOCKFILE || exit 1
flock -n "$lock_fd" || {
echo "Another instance is running" >&2
exit 1
}
# Note that the fd will unlock automatically at the end of the script.
# Unlock once all background processes are done.
exec {lock_fd}>&-
# Or unlock immediately
flock -u "$lock_fd"