# Pastebin 9m8mK5jb #!/bin/sh -eu set -o pipefail main() { acquire_lock do_stuff } acquire_lock() { lock="/tmp/lock" # open lockfile which will be released when script exits exec 9>"${lock}" if ! lockf -s -t 5 9; then echo "failed to acquire lock" exit 1 fi } do_stuff() { echo -n "doing..." sleep 10 echo "stuff" } main