Linux lsof 命令例子

Published 03-04-2019 16:32:44

ls + of 表示 list open files 是一个列出当前系统打开文件的工具

lsof 列出所有打开的文件

不加任何参数列表所有

COMMAND     PID  TID       USER   FD      TYPE DEVICE SIZE/OFF      NODE NAME
skynet     3591          deploy  cwd       DIR  252,0     4096   1836864 /data/deploy/cc_server
skynet     3591          deploy  rtd       DIR  252,0     4096         2 /
skynet     3591          deploy  txt       REG  252,0  3063719 107220195 /usr/lib/skynetx/sx2.0/bin/skynet
skynet     3591          deploy  mem       REG  252,0    40960 107220216 /usr/lib/skynetx/sx2.0/csrc/netpack.so
skynet     3591          deploy  mem       REG  252,0    38136 107220224 /usr/lib/skynetx/sx2.0/csrc/cjson.so
skynet     3591          deploy  mem       REG  252,0    58362 106955562 /usr/lib/skynetx/sx2.0/
skynet     3591          deploy  mem       REG  252,0    15242 106955568 /usr/lib/skynetx/sx2.0/cservice/logger.so
skynet     3591          deploy  mem       REG  252,0  1840928  14946124 /lib/x86_64-linux-gnu/libc-2.19.so
skynet     3591          deploy  mem       REG  252,0   149120  14946117 /lib/x86_64-linux-gnu/ld-2.19.so
skynet     3591          deploy    0r     FIFO   0,10      0t0    740902 pipe
skynet     3591          deploy    1w     FIFO   0,10      0t0    740903 pipe
skynet     3591          deploy    2w     FIFO   0,10      0t0    740904 pipe
skynet     3591          deploy    3u     0000   0,11        0      6949 anon_inode
skynet     3591          deploy    4r     FIFO   0,10      0t0    740908 pipe
skynet     3591          deploy    5w     FIFO   0,10      0t0    740908 pipe
skynet     3591          deploy    6u     IPv4 745693      0t0       TCP localhost:9751 (LISTEN)
skynet     3591          deploy    8u     IPv4 734160      0t0       TCP localhost:49596->localhost:6379 (ESTABLISHED)
skynet     3591          deploy    9u     IPv4 734161      0t0       TCP localhost:49598->localhost:6379 (ESTABLISHED)

FD 表示文件描述符, 一般有下面几个

  • cwd – 应用程序的当前工作目录
  • txt – 该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /usr/lib/skynetx/sx2.0/bin/skynet
  • mem – Memory mapped file
  • mmap – Memory mapped device
  • 1u 3u 4r 表示实例的文件描述符 r 表示read, w 表示write, u 表示read 和 write

TYPE 列表示文件类型

  • REG – 普通文件
  • DIR – 表示目录
  • FIFO –先进先出 (FIFO) 队列
  • CHR – 表示字符类型

列出进程打开的文件

lsof -p 3591

-p 参数 进程pid

deploy@caocao-ci:~$ lsof -p 3591 | more
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
skynet  3591 deploy  cwd    DIR  252,0     4096   1836864 /data/deploy/cc_server
skynet  3591 deploy  rtd    DIR  252,0     4096         2 /
skynet  3591 deploy  txt    REG  252,0  3063719 107220195 /usr/lib/skynetx/sx2.0/bin/skynet
skynet  3591 deploy  mem    REG  252,0    40960 107220216 /usr/lib/skynetx/sx2.0/csrc/netpack.so

列出一个目录下打开的文件

lsof +D /data/deploy/cc_server/

+D 会递归子目录,+d 不会递归子目录

deploy@caocao-ci:~$ lsof +D /data/deploy/cc_server/
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
skynet  3591 deploy  cwd    DIR  252,0     4096 1836864 /data/deploy/cc_server
skynet  3591 deploy 2019r   REG  252,0     1708 1839600 /data/deploy/cc_server/data/client/6477/files.json
skynet  3591 deploy 2025r   REG  252,0     1708 1839600 /data/deploy/cc_server/data/client/6477/files.json
skynet  3591 deploy 2029r   REG  252,0     1708 1839600 /data/deploy/cc_server/data/client/6477/files.json
skynet  3591 deploy 2031r   REG  252,0     1708 1839600 /data/deploy/cc_server/data/client/6477/files.json

列出所有的网络连接 lsof -i

deploy@caocao-ci:~$ lsof -i | more
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python  2607 deploy    3u  IPv4 458753      0t0  TCP localhost:38814->localhost:6379 (ESTABLISHED)
skynet  3591 deploy    6u  IPv4 745693      0t0  TCP localhost:9751 (LISTEN)
skynet  3591 deploy    8u  IPv4 734160      0t0  TCP localhost:49596->localhost:6379 (ESTABLISHED)
skynet  3591 deploy    9u  IPv4 734161      0t0  TCP localhost:49598->localhost:6379 (ESTABLISHED)
skynet  3591 deploy   10u  IPv4 734162      0t0  TCP localhost:49600->localhost:6379 (ESTABLISHED)
skynet  3591 deploy   11u  IPv4 734163      0t0  TCP localhost:49602->localhost:6379 (ESTABLISHED)

指定端口 协议

deploy@caocao-ci:~$ lsof -i :9700
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
skynet  3591 deploy 2012u  IPv4 754818      0t0  TCP *:9700 (LISTEN)

也可以指定tcp udp

deploy@caocao-ci:~$ lsof -i tcp:9700
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
skynet  3591 deploy 2012u  IPv4 754818      0t0  TCP *:9700 (LISTEN)
deploy@caocao-ci:~$ lsof -i udp:9700

如上 使用 upd:9700 时 没有结果,tcp:9700 列表了进程 skynet

只针对tcp 协议

deploy@caocao-ci:~$ lsof -i tcp | more
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python  2607 deploy    3u  IPv4 458753      0t0  TCP localhost:38814->localhost:6379 (ESTABLISHED)
skynet  3591 deploy    6u  IPv4 745693      0t0  TCP localhost:9751 (LISTEN)
skynet  3591 deploy    8u  IPv4 734160      0t0  TCP localhost:49596->localhost:6379 (ESTABLISHED)

repeat 模式 每隔一段时间执行lsof指令

-r5 表示5秒执行一次

deploy@caocao-ci:~$ lsof -u deploy  -a -r5

根据进程名

-c 选项 根据程序名字

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
skynet  3591 deploy  cwd    DIR  252,0     4096   1836864 /data/deploy/cc_server
skynet  3591 deploy  rtd    DIR  252,0     4096         2 /
skynet  3591 deploy  txt    REG  252,0  3063719 107220195 /usr/lib/skynetx/sx2.0/bin/skynet
skynet  3591 deploy  mem    REG  252,0    40960 107220216 /usr/lib/skynetx/sx2.0/csrc/netpack.so
skynet  3591 deploy  mem    REG  252,0     8451 107220212 /usr/lib/skynetx/sx2.0/csrc/myrand.so
skynet  3591 deploy  mem    REG  252,0   114120 107220218 /usr/lib/skynetx/sx2.0/csrc/sproto.so
skynet  3591 deploy  mem    REG  252,0   171964 107220209 /usr/lib/skynetx/sx2.0/csrc/lpeg.so