Write a script that prevents two copies of itself from running at the same time (e.g. from cron).

AdvancedHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
exec 200>/var/lock/nightly-sync.lock
flock -n 200 || { echo "Another run is in progress, exiting"; exit 0; }

# ... actual work ...
rsync -a /data/ backup:/data/

Key trick → flock locks a file descriptor that the kernel releases when the process dies. A PID file can go stale after a crash, a flock can't.

All Real-World Interview questions Roadmap: Interview readiness

More on Bash scripting tasks