From 2d8a89d6be28c995478794531748e4c7b0a3b9d4 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Tue, 20 Jun 2023 14:18:54 +0200 Subject: Added naive wrapper to run scripts as singletons. * bin/singleton: Added. --- bin/singleton | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 bin/singleton 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 $@ -- cgit v1.2.3