Linux文件锁相关命令

1.命令lslocks

# lslocks 
COMMAND            PID   TYPE SIZE MODE  M START END PATH
rpcbind            838  FLOCK      WRITE 0     0   0 /run/rpcbind/rpcbind.lock
(unknown)         1027  FLOCK      WRITE 0     0   0 /run...
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_1.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_2.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_3.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_4.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_5.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_6.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_7.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_8.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_9.ibt
mysqld            1265  POSIX  80K WRITE 0     0   0 /var/lib/mysql/#innodb_temp/temp_10.ibt
java              1316  POSIX      WRITE 0     0   0 /var/lib/elasticsearch/nodes/0/indices/VkoqTksiSIuH3fXWXmWrQA/0/index/write.lock
prometheus        1098  FLOCK      WRITE 0     0   0 /var/lib/prometheus/data/lock
mysqld            1265  POSIX  24M WRITE 0     0   0 /var/lib/mysql/mysql.ibd
dockerd           1313  FLOCK  16K WRITE 0     0   0 /var/lib/docker/builder/fscache.db
dockerd           1313  FLOCK  16K WRITE 0     0   0 /var/lib/docker/buildkit/snapshots.db
dockerd           1313  FLOCK  16K WRITE 0     0   0 /var/lib/docker/buildkit/metadata.db
dockerd           1313  FLOCK  32K WRITE 0     0   0 /var/lib/docker/buildkit/cache.db
java              1316  POSIX      WRITE 0     0   0 /var/lib/elasticsearch/nodes/0/snapshot_cache/write.lock
java              1316  POSIX      WRITE 0     0   0 /var/lib/elasticsearch/nodes/0/_state/write.lock
java              1316  POSIX      WRITE 0     0   0 /var/lib/elasticsearch/nodes/0/indices/qE9fxJSUQbe5o6fm5VGOsQ/0/index/write.lock
java              1316  POSIX      WRITE 0     0   0 /var/lib/elasticsearch/nodes/0/indices/Su4NAfTeQLmziVL08HJzmw/0/index/write.lock
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/network/driver.pid
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/interface/driver.pid
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/secrets/driver.pid
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/storage/driver.pid
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/nodedev/driver.pid
libvirtd        197008  POSIX   6B WRITE 0     0   0 /run/libvirt/nwfilter/driver.pid
(undefined)         -1 OFDLCK      READ  0     0   0 /dev...
containerd        1578  FLOCK  32K WRITE 0     0   0 /var/lib/docker/containerd/daemon/io.containerd.metadata.v1.bolt/meta.db
mysqld            1265  POSIX 192K WRITE 0     0   0 /var/lib/mysql/#ib_16384_0.dblwr
mysqld            1265  POSIX 8.2M WRITE 0     0   0 /var/lib/mysql/#ib_16384_1.dblwr
dockerd           1313  FLOCK  32K WRITE 0     0   0 /var/lib/docker/volumes/metadata.db
mysqld            1265  POSIX  12M WRITE 0     0   0 /var/lib/mysql/ibdata1

可以看到系统中当前所有锁定的文件。我们还可以看到每个锁的详细信息,比如锁类型、哪个进程持有锁。

2. /proc/locks

# cat  /proc/locks
1: POSIX  ADVISORY  WRITE 197008 00:18:122608 0 0
2: FLOCK  ADVISORY  WRITE 154427 00:18:107039 0 EOF
3: POSIX  ADVISORY  WRITE 1316 fd:00:4206288 0 EOF
4: FLOCK  ADVISORY  WRITE 1316 00:29:31 0 EOF
5: FLOCK  ADVISORY  WRITE 1357 00:18:1144 0 EOF
6: FLOCK  ADVISORY  WRITE 968 00:18:999 0 EOF
7: POSIX  ADVISORY  WRITE 197008 00:18:122620 0 0
8: POSIX  ADVISORY  WRITE 1316 fd:00:4206066 0 EOF
9: POSIX  ADVISORY  WRITE 1316 fd:00:4206121 0 EOF
10: POSIX  ADVISORY  WRITE 1316 fd:00:4206163 0 EOF
11: POSIX  ADVISORY  WRITE 1316 fd:00:4206052 0 EOF
12: POSIX  ADVISORY  WRITE 1316 fd:00:4206016 0 EOF
13: POSIX  ADVISORY  WRITE 1265 fd:00:3932382 0 EOF
14: POSIX  ADVISORY  WRITE 1265 fd:00:3934271 0 EOF
15: POSIX  ADVISORY  WRITE 1265 fd:00:3932295 0 EOF
16: POSIX  ADVISORY  WRITE 1265 fd:00:3932266 0 EOF
17: FLOCK  ADVISORY  WRITE 1313 fd:00:4206362 0 EOF
18: POSIX  ADVISORY  WRITE 1265 fd:00:3932273 0 EOF
19: POSIX  ADVISORY  WRITE 1265 fd:00:3932272 0 EOF
20: FLOCK  ADVISORY  WRITE 1578 fd:00:4206294 0 EOF
21: OFDLCK ADVISORY  READ -1 00:05:4 0 EOF
22: POSIX  ADVISORY  WRITE 197008 00:18:122618 0 0

第一列是序列号。
第二个字段表示所使用的锁的类,例如FLOCK(来自flock系统调用)或POSIX(来自lockf、fcntl系统调用)。
第三个字段此列用于显示锁的类型。 它可以有两个值:ADVISORY 或 MANDATORY。
第四个字段显示锁是写锁还是读锁。
第五持有锁的进程的 ID。
第六个以“major-device:minor-device:inode”格式显示锁定文件的 id。
最后两列表示被锁定文件的锁定区域的开始和结束位置。 0 到EOF表示整个文件被锁定。

3.flock使用

 可以对文件加锁。

 -s, --shared             get a shared lock
 -x, --exclusive          get an exclusive lock (default)
 -u, --unlock             remove a lock
 -n, --nonblock           fail rather than wait
 -w, --timeout      wait for a limited amount of time
 -E, --conflict-exit-code   exit code after conflict or timeout
 -o, --close              close file descriptor before running command
 -c, --command   run a single command string through the shell
 -F, --no-fork            execute command without forking
     --verbose            increase verbosity

 -h, --help               display this help

你可能感兴趣的:(linux,linux,运维,服务器)