Posts

Showing posts from November, 2016

Linux - Locking a file

You could use the following command to lock a Linux file


#Open myfile as file descriptor 2
$ exec 2>myfile
#lock the file descriptor 2
$ flock -x 2
And release the lock with the following

$ exec 2>&-
Then, you could show file descriptor holding with the following command

lsof | grep myfile
or

lsof myfile
It could return something like

bash        132  userX    2wW   REG    8,2    0 135612 myfile
The following is the man page from lsof


FD
is the File Descriptor number of the file or:
cwd current working directory; Lnn library references (AIX); err FD information error (see NAME column); jld jail directory (FreeBSD); ltx shared library text (code and data); Mxx hex memory-mapped type number xx. m86 DOS Merge mapped file; mem memory-mapped file; mmap memory-mapped device; pd parent directory; rtd root directory; tr kernel trace file (OpenBSD); txt program text (code and data); v86 VP/ix mapped file;
FD is followed by one of these characters, describing the mode under which the…