#!/bin/bash # === global variables ================================================ LOCKBASE="/var/lock" # old #LOCKBASE="/run/lock" # new # === support functions =============================================== # --- display usage information --------------------------------------- action_usage() { BN=$(basename $0) cat <&2 exit 1 fi if [ -z "${CALLFUNC}" ] then echo "Error: no function to call specified!" >&2 exit 1 fi # - - locking section - begin - - - - - - - - - - - - - - - - - - - - - ( flock -x -n 234 if [ "$?" = "0" ] then # call the important function within the locked context "${CALLFUNC}" $* # clear lockfile, because we must have created it rm "${LOCKFILE}" # don't do any important stuff here exit 0 else echo "Error: Failed to acquire lock!" >&2 exit 1 fi ) 234>"${LOCKFILE}" # - - locking section - end - - - - - - - - - - - - - - - - - - - - - - } # === main entry point ================================================ case $1 in nt|no-lock-test) shift action_no_lock_test $1 ;; lt|lock-test) shift action_lock_test "${LOCKBASE}/script-lock-test.lck" "action_no_lock_test" "$1" ;; help) action_usage action_help ;; *) action_usage exit 1 ;; esac exit 0 # --- end of file ---