I would like to get a list of pid's which hold shared lock on /tmp/file. Is this possible using simple command line tools?
2 Answers
From
man lsof:FD is the File Descriptor number of the file or: FD is followed by one of these characters, describing the mode under which the file is open:
The mode character is followed by one of these lock characters, describing the type of lock applied to the file: R for a read lock on the entire file; W for a write lock on the entire file; space if there is no lock.
So R in 3uR mean that read/shared lock is issued by 613 PID.
#lsof /tmp/file COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME perl 613 turkish 3uR REG 8,2 0 1306357 /tmp/file Reading directly from
/proc/locksis faster thanlsof,perl -F'[:\s]+' -wlanE' BEGIN { $inode = (stat(pop))[1]; @ARGV = "/proc/locks" } say "pid:$F[4] [$_]" if $F[7] == $inode ' /tmp/file
fuser /tmp/file will list all processes which have the file open, including those that have a lock on it.
- Yes, and
lsofis also aware of that butflockcan refuse locking, or pid could wait for lock after file opening.mpapec– mpapec2013-09-14 11:33:49 +00:00Commented Sep 14, 2013 at 11:33 - No, it will not necessary list all processes, only processes on the same System. This can break for network storage.12431234123412341234123– 124312341234123412341232022-11-21 11:31:10 +00:00Commented Nov 21, 2022 at 11:31