linux,

File locking with flock 101

Nov 25, 2021 · 2 mins read · Post a comment
File locking with flock 101

As a concept, file locking in Linux purpose is to control access to a shared resource between multiple processes, in a way to indicate which process is next in line so it could use the shared resource (file), also known as serialization. To ensure the serialization, a cooperation between processes is required, and before I give an example, keep in mind that multiple processes could hold a read lock simultaneously, and only one can have a write lock at a time. A cooperation means processes respects each others turns, so if process_1 is writing to a shared file (acquires a write lock), and process_2 decides to write in the same file as well without trying to acquire a write lock too, process_2 is not a cooperating one.
Since there are two types of Linux locks, advisory and mandatory, and the second type is unreliable, by default advisory locks are used. These locks are also called “unenforced”.

Now, flock is a utility which manages locks and could be used with shell scripts or from the terminal.

Prerequisites

  • Linux shell environment

List file locks

List all held file locks at the moment.

$ lslocks

Example output:

COMMAND           PID  TYPE SIZE MODE  M START END PATH
systemd-timesyn   780 FLOCK      WRITE 0     0   0 /run...
lvmetad           492 POSIX   4B WRITE 0     0   0 /run/lvmetad.pid
cron             1173 FLOCK   5B WRITE 0     0   0 /run/crond.pid

This information is pulled from the /proc/locks file.

flock example

Example backing up and uploading a Postgres database to S3 (as part of a shell script):

flock -xn /tmp/cron.lock -c "pg_dump -Fc devcoops_db > /db_backups/devcoops_db.sql"
flock -xn /tmp/cron.lock -c "aws s3 cp /db_backup/devcoops_db s3://devcoops/devcoops_db.sql"

So, in this case, the pg_dump command will finish first before aws s3 cp starts, as we want the flow to be.

  • -x stands for exclusive (write) lock.
  • -n fail immediately, rather than waiting for lock acquiring.
  • -c passing a command.

Conclusion

To get more details regarding flock run man flock. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.