Linux(Centos 7.6)命令详解:lsof

1.命令作用

列出打开的文件(list open files);列出系统中所有活动进程打开的文件

2.命令语法

Usage: lsof [options]

3.参数详解

OPTION:

  • -a,多个参数按照且进行匹配计算,默认是或;如 lsof -u root -c postmaster -a
  • -c cmd,列出指定进程名称所打开的文件
  • +d dir,列出目录下被打开的文件
  • +D dir,递归列出目录下被打开的文件
  • -d,从输出列表中排除或包含的文件描述符
  • -i,列出符合条件的进程
  • -p,列出进程号所打开的文件息
  • -u,列出uid号进程详情

4.常用用例

4.1.列出指定文件关联的进程

[root@host49 test2]# nmon -f -s 10 -c 30               # 运行一个后台运行的命令,其他命令也可以
[root@host49 test2]# 
[root@host49 test2]# lsof host49_231018_1804.nmon  # 查看nmon生成文件涉及的进程
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
nmon    151896 root   11w   REG    8,3   177460 66980903 host49_231018_1804.nmon
[root@host49 test2]# 
[root@host49 test2]# ps -ef | grep nmon
root     151896      1  0 18:04 pts/10   00:00:00 nmon -f -s 10 -c 30
root     152015 150325  0 18:04 pts/10   00:00:00 grep --color=auto nmon
[root@host49 test2]# 

4.2.列出指定程序打开的文件

[root@host49 test2]# lsof -c nmon
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF       NODE NAME
nmon    151896 root  cwd    DIR    8,3     4096   66994193 /root/llll/test2
nmon    151896 root  rtd    DIR    8,3     4096          2 /
nmon    151896 root  txt    REG    8,3   158216   82859771 /usr/bin/nmon
[root@host49 test2]# 

4.3.列出指定端口号进程

[root@node4 ~]# lsof -i:5432
COMMAND     PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
postmaste 10554 postgres    6u  IPv4  78366      0t0  TCP *:postgres (LISTEN)
postmaste 10554 postgres    7u  IPv6  78367      0t0  TCP *:postgres (LISTEN)
[root@node4 ~]# 

4.4.列出指定进程ID打开的文件

[root@node4 ~]# lsof -p 10554
COMMAND     PID     USER   FD   TYPE             DEVICE  SIZE/OFF     NODE NAME
postmaste 10554 postgres  cwd    DIR                8,3      4096 50526377 /home/postgres/data
postmaste 10554 postgres  rtd    DIR                8,3      4096       64 /
postmaste 10554 postgres  txt    REG                8,3   8457168  2884220 /usr/pgsql-15/bin/postgres
postmaste 10554 postgres  DEL    REG                0,4              78358 /dev/zero
postmaste 10554 postgres  mem    REG                8,3 106075056 17065797 /usr/lib/locale/locale-archive
... ...
... ...
postmaste 10554 postgres  DEL    REG                0,4             950283 /SYSV0302f8a9
postmaste 10554 postgres    0r   CHR                1,3       0t0     1028 /dev/null
postmaste 10554 postgres    1w  FIFO                0,9       0t0    78362 pipe
postmaste 10554 postgres    2w  FIFO                0,9       0t0    78362 pipe
postmaste 10554 postgres    3r  FIFO                0,9       0t0    78361 pipe
postmaste 10554 postgres    4w  FIFO                0,9       0t0    78361 pipe
postmaste 10554 postgres    5r  FIFO                0,9       0t0    78362 pipe
postmaste 10554 postgres    6u  IPv4              78366       0t0      TCP *:postgres (LISTEN)
postmaste 10554 postgres    7u  IPv6              78367       0t0      TCP *:postgres (LISTEN)
postmaste 10554 postgres    8u  unix 0xffff8c76d8493400       0t0    78369 /var/run/postgresql/.s.PGSQL.5432
postmaste 10554 postgres    9u  unix 0xffff8c76d8494800       0t0    78371 /tmp/.s.PGSQL.5432
[root@node4 ~]# 

4.5.列出指定用户名称/UID的进程

[root@node4 ~]# lsof -u postgres | head
COMMAND     PID     USER   FD      TYPE             DEVICE  SIZE/OFF     NODE NAME
postmaste 10554 postgres  cwd       DIR                8,3      4096 50526377 /home/postgres/data
postmaste 10554 postgres  rtd       DIR                8,3      4096       64 /
postmaste 10554 postgres  txt       REG                8,3   8457168  2884220 /usr/pgsql-15/bin/postgres
postmaste 10554 postgres  DEL       REG                0,4              78358 /dev/zero
postmaste 10554 postgres  mem       REG                8,3 106075056 17065797 /usr/lib/locale/locale-archive
postmaste 10554 postgres  mem       REG                8,3     11448   195623 /usr/lib64/libfreebl3.so
postmaste 10554 postgres  mem       REG                8,3     68192   229201 /usr/lib64/libbz2.so.1.0.6
postmaste 10554 postgres  mem       REG                8,3    100008   235183 /usr/lib64/libelf-0.172.so
postmaste 10554 postgres  mem       REG                8,3    402384   214695 /usr/lib64/libpcre.so.1.2.0
[root@node4 ~]#

4.6.列出目录下打开的进程

lsof +d /root
lsof +D /root
[root@node4 ~]# lsof +d /root | head
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
gnome-ses  7374 root  cwd    DIR    8,3     4096 33574977 /root
dbus-laun  7383 root  cwd    DIR    8,3     4096 33574977 /root
dbus-daem  7384 root  cwd    DIR    8,3     4096 33574977 /root
gvfsd      7460 root  cwd    DIR    8,3     4096 33574977 /root
gvfsd-fus  7465 root  cwd    DIR    8,3     4096 33574977 /root
at-spi-bu  7582 root  cwd    DIR    8,3     4096 33574977 /root
dbus-daem  7587 root  cwd    DIR    8,3     4096 33574977 /root
at-spi2-r  7589 root  cwd    DIR    8,3     4096 33574977 /root
gnome-she  7619 root  cwd    DIR    8,3     4096 33574977 /root
[root@node4 ~]# 

5.7.删除文件后,磁盘空间没有回收

注意: tail进程结束后,空间也会自动释放

1.会话1: 使用tail -f 命令,读取一个文件
df -h /data1
tail -f a.txt

2.会话2: 在另外一个窗口,删除a.txt文件;df 查看磁盘空间,发现空间没有变化
rm -rf a.txt
df -h /data1

3.通过lsof |grep a.txt 查看有哪些进程在访问a.txt
lsof | grep a.txt
# 输出如下:
tail    4043    root    3r  REG       8,3         9   38288842 /data1/a.txt (deleted)

ll /proc/4043/fd                            # 可以查看到删除的文件,所以空间并没有释放
# 输出如下:(3链接到一个文件,处于deleted状态,且显示是红色闪烁状态)
lr-x------ 1 root root 64 Aug 10 19:56 3 -> '/data1/a.txt(deleted)'

4.执行echo 3 命令,彻底释放磁盘空间
df -f /data1
echo 3 >/proc/4043/fd/3
df -f /data1

4.8.恢复删除的文件

注意: 前提是删除的文件 file 通过 lsof 查看是deleted状态

1.会话1: 使用tail -f 命令,读取一个文件
tail -f a.txt

2.会话2: 在另外一个窗口,删除a.txt文件
md5sum a.txt
rm -rf a.txt

3.通过lsof |grep a.txt 查看有哪些进程在访问a.txt
lsof |grep a.txt
# 输出信息:(最后是deleted状态,是可以恢复的)
tail    4043    root    3r  REG       8,3         9   38288842 /data1/a.txt (deleted)

4.执行执行cat命令,恢复被删除的文件,并用md5sum验证文件是否正确
cat /proc/4385/fd/3 > /data1/a.txt
md5sum a.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

豆是浪个

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

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

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

打赏作者

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

抵扣说明:

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

余额充值