Self-locking script

For anything cron or a login hook might start twice (from the flock man page): the script re-executes itself under flock, using its own file as the lock. The second invocation exits instead of racing the first.

[ "${FLOCKER:-}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

-e exclusive, -n fail instead of queue; drop -n to wait. The || : pins the guard line’s own exit status to 0, without it, a script whose last line is this guard reports failure. (set -e never sees the failing test: it is a non-final member of an && list, which set -e exempts.)

Where it breaks: flock is util-linux (absent on stock macOS), and advisory locks are unreliable on network filesystems, on NFS or AFS, lock a file in /tmp instead of "$0". Contention exits silently; log before || : if you need to know.