bash remove lockfile with trap

#!/bin/bash
LOCKFILE=/var/lock/makewhatis.lock
[ -f $LOCKFILE ] && exit 0
# Upon exit, remove lockfile.
trap "{ rm -f $LOCKFILE ; exit 255; }" EXIT
touch $LOCKFILE
#do sth. here
exit 0

taken from ‘bash guide for beginners’

Leave a Reply