监控文件锁,使用flock锁定

mpa*_*pec 6 lock files inotify

我想获得一个持有共享锁的pid列表/tmp/file。这可以使用简单的命令行工具吗?

mpa*_*pec 5

  • 来自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.
    
    Run Code Online (Sandbox Code Playgroud)

因此,R3uR平均读取/共享锁由发出613PID。

#lsof /tmp/file
COMMAND PID    USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
perl    613 turkish    3uR  REG    8,2        0 1306357 /tmp/file
Run Code Online (Sandbox Code Playgroud)
  • 直接从 读取/proc/lockslsof

    perl -F'[:\s]+' -wlanE'
      BEGIN { $inode = (stat(pop))[1]; @ARGV = "/proc/locks" }
      say "pid:$F[4] [$_]" if $F[7] == $inode
    ' /tmp/file
    
    Run Code Online (Sandbox Code Playgroud)