flock

noun

say it: “EF-lok” /ˈɛflɑk/

A Linux command that holds a lock on a file, so a second copy of a script can see the first is still running and skip.

What does flock do?

flock takes a lock on a file so only one copy of a script runs at a time. If last night's backup is still running when the next one starts, the second one finds the lock taken and quietly skips. -n means don't wait.

exec 9> backup.lock
if ! flock -n 9; then
  echo "Another backup is still running. Skipping."
  exit 0
fi

In a sentence

  • “Sam added three flock lines to the top of backup.sh.”
  • “With flock, the second backup saw the lock taken and skipped.”

Level: Freelancer · More Unix terms at this level

Updated