Simple BASH script lockfiles

LOCKFILE=/var/lock/my-script.lock
 
# Check if we're already running
(set -C; : > $LOCKFILE) 2> /dev/null
if [ $? != "0" ]; then
   echo "Lockfile exists, exiting"
   exit 1
fi
 
trap 'rm $LOCKFILE' EXIT
 
#rest of script goes here

WordPress Pastebin plugin

About James

If this were the 80s I'd be sat in front of a C64 or Speccy, or taking VCRs apart.