aboutsummaryrefslogtreecommitdiff
path: root/bin/singleton
blob: e11ed71e1c051ef9d0bf398580da8bcb883bc174 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 $@