Files

監控文件鎖,使用flock鎖定

  • July 7, 2016

我想獲得一個持有共享鎖的pid列表/tmp/file。這可以使用簡單的命令行工具嗎?

  • 來自man lsof

FD 是文件的文件描述符編號或: FD 後跟以下字元之一,描述文件打開的模式:

         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.

所以意味著讀/共享鎖是由PID發出R的。3uR``613

#lsof /tmp/file
COMMAND PID    USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
perl    613 turkish    3uR  REG    8,2        0 1306357 /tmp/file

  • 直接讀取/proc/lockslsof,
perl -F'[:\s]+' -wlanE'
 BEGIN { $inode = (stat(pop))[1]; @ARGV = "/proc/locks" }
 say "pid:$F[4] [$_]" if $F[7] == $inode
' /tmp/file

引用自:https://unix.stackexchange.com/questions/90506