How to perform a lock/mutex in bash Feb 3, 2020 This can be done using the ln command: tempfile=$(mktemp ./lock.XXXXXX) lockfile=./lockfile trap "rm $lockfile" EXIT try=1 while ! ln $tempfile $lockfile > /dev/null 2>&1; do try=$[ $try + 1 ] if [[ $try -gt 10 ]]; then echo "ERROR: Timeout trying to lock $cfgfile" rm $tempfile exit 1 fi sleep 1 done rm $tempfile # Protected section