diff options
-rwxr-xr-x | bin/singleton | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/bin/singleton b/bin/singleton new file mode 100755 index 0000000..e11ed71 --- /dev/null +++ b/bin/singleton @@ -0,0 +1,17 @@ +#!/bin/sh +# +# Simple script wrapper to force another script to run as a singleton: +# prevents multiple instances of it (the same command name; naive) +# from running. + +LOCKFILE="/tmp/singleton-$1" +LOCKFD=9 + +lock() { flock -$1 $LOCKFD; } +stop_lock() { lock u; lock xn && rm -f $LOCKFILE; } +prep_lock() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap stop_lock EXIT; } + +prep_lock +lock xn || exit 1 + +exec $@ |