Search code examples
linuxfileflock

Unable to lock file linux


I'm trying to lock a file and obviously there is something I'm missing, because eventhough it seems it's locked I can still access and edit it using vim editor.

Locking file:

flock -x lock2.txt sleep 30

Checking using lslocks:

COMMAND           PID  TYPE SIZE MODE  M START END PATH
flock            5417 FLOCK  10B WRITE 0     0   0 /home/lock2.txt

But still able to access it and edit using different terminal (different process I believe). I tried the solution with file descriptors here Linux flock, how to "just" lock a file? but the result is still the same.


Solution

  • The flock command uses flock(2) to lock the file. As the documentation says

    flock() places advisory locks only; given suitable permissions on a file, a process is free to ignore the use of flock() and perform I/O on the file.

    In general, applications don't check advisory locks. They're intended for use within a specific application to coordinate between multiple processes.

    The flock command is most often used by a single application just to prevent itself from running multiple times concurrently.