Linux文件锁相关命令

文章介绍了Linux系统中的文件锁定情况,通过lslocks命令展示当前锁定的文件及其详细信息,以及/proc/locks中锁的状态,包括锁类型、进程、文件ID等。同时讲解了flock命令的使用方法。
摘要由CSDN通过智能技术生成

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 <secs>     wait for a limited amount of time
 -E, --conflict-exit-code <number>  exit code after conflict or timeout
 -o, --close              close file descriptor before running command
 -c, --command <command>  run a single command string through the shell
 -F, --no-fork            execute command without forking
     --verbose            increase verbosity

 -h, --help               display this help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

daolongzhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值