Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 912 Bytes

flock.md

File metadata and controls

27 lines (22 loc) · 912 Bytes

flock

Linux has a flock command to manage file locks from shell scripts.

One example from https://linuxaria.com/ is to lock a file in a script and allow it to automatically go out of scope and release itself when the file gets closed.

#!/bin/bash

lock="/var/run/file"

# exec can also be used to open a file and name a file handle for it.
# The call exec 200>"${lock}" will open the file named in $lock for reading
# and assign it file descriptor 200.
exec 200>"${lock}"
# This tells flock to exclusively lock the file referenced by file handle 200
# or exit with code 1.
# The state of being locked lasts after the flock call because the file
# handle is still valid.
# That state will last until the file handle is closed - in this case until
# the script exits.
flock -n 200 || exit 1