Recently Written · git

tmpl

Insanely simple templates

git clone https://github.com/equwal/tmpl

Log | Files | Refs


sh/trap/lock-file (289 bytes)

1 # Exclusive lock using trap
2 lockfile="/var/run/<++>.lock"
3 
4 cleanup() {
5     rm -f "$lockfile"
6 }
7 
8 # Try to acquire lock
9 if ! (set -C; echo $$ > "$lockfile") 2>/dev/null; then
10     echo "Script is already running" >&2
11     exit 1
12 fi
13 
14 trap cleanup EXIT TERM INT
15 
16 # Your exclusive work here
17 <++>