Skip to content
Advertisement

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:

JavaScript

Checking using lslocks:

JavaScript

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement