How do you write robust production shell scripts with locking and atomic writes?

Answer

Production-grade scripts require several patterns. Exclusive locking with flock prevents multiple instances running simultaneously: exec 9>/var/lock/myscript.lock; flock -n 9 || { echo "Already running"; exit 1; } — the lock is automatically released when the script exits. Atomic writes prevent readers from seeing partially-written files: write to a temp file on the same filesystem then rename: TMP=$(mktemp /target/dir/.tmp.XXXXXX); write_data > "$TMP" && mv "$TMP" /target/filemv within a filesystem is atomic at the kernel level. Additional robustness patterns: use set -euo pipefail, validate all inputs before use, use trap for cleanup, log with timestamps to a dedicated log file, never use ls output in scripts (use globs or find instead), quote all variable expansions, and use shellcheck in CI to catch common errors automatically.