Last active
October 16, 2024 09:15
-
-
Save przemoc/571091 to your computer and use it in GitHub Desktop.
Lockable script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# SPDX-License-Identifier: MIT | |
## Copyright (C) 2009 Przemyslaw Pawelczyk <[email protected]> | |
## | |
## This script is licensed under the terms of the MIT license. | |
## https://opensource.org/licenses/MIT | |
# | |
# Lockable script boilerplate | |
### HEADER ### | |
LOCKFILE="/var/lock/`basename $0`" | |
LOCKFD=99 | |
# PRIVATE | |
_lock() { flock -$1 $LOCKFD; } | |
_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; } | |
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; } | |
# ON START | |
_prepare_locking | |
# PUBLIC | |
exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail | |
exlock() { _lock x; } # obtain an exclusive lock | |
shlock() { _lock s; } # obtain a shared lock | |
unlock() { _lock u; } # drop a lock | |
### BEGIN OF SCRIPT ### | |
# Simplest example is avoiding running multiple instances of script. | |
exlock_now || exit 1 | |
# Remember! Lock file is removed when one of the scripts exits and it is | |
# the only script holding the lock or lock is not acquired at all. |
This is beautiful. Thank you.
This is very nice.
If you don't mind the lockfile staying around, this is very simple and easy in bash and probably fine in sh (not tested):
exec 9>>mylockfile
if ! flock --exclusive --nonblock 9; then
echo "$(date -u +"%FT%T.%3NZ") $$: scriptname will wait for lock"
flock --exclusive 9
echo "$(date -u +"%FT%T.%3NZ") $$: scriptname finished waiting for lock"
fi
... this code is protected against concurrency ...
It's easy enough to adjust that to exit if the script is already running:
exec 9>>mylockfile
if ! flock --exclusive --nonblock 9; then
echo "scriptname already running, exiting"
exit 1
fi
... this code is protected against concurrency ...
The lock is automatically released once the file descriptor is closed (which is inherited by subshells or things you exec from the shellscript, so all that needs to close).
Thus the rest of the script is protected against concurrent execution.
There is no issues with power loss and the lockfile staying around either.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will always be true. kill -9 is "non-catchable, non-ignorable kill", the OS won't let you do anything when you get one, it's the equivalent of pulling taking an axe to the power supply. It's why you should escalate to
kill -9
when trying to fix something, not start there.