linux bug定位神器——lsof | grep 简直不能太好用

在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。
因为应用程序打开文件的描述符列表提供了大量关于这个应用程序本身的信息,因此通过lsof工具能够查看这个列表对系统监测以及排错将是很有帮助的。

lsof | grep 简直不能太好用

COMMAND  PID  USER   FD  TYPE  DEVICE  SIZE/OFF    NODE NAME
bash    2811  devl  cwd   DIR   253,1      4096   81927 /home/devl
bash    2811  devl  rtd   DIR   253,1      4096       2 /
bash    2811  devl  txt   REG   253,1    964608  327823 /usr/bin/bash
bash    2811  devl  mem   REG   253,1   2151672  328342 /usr/lib64/libc-2.17.so
bash    2811  devl  mem   REG   253,1     19288  328814 /usr/lib64/libdl-2.17.so
bash    2811  devl  mem   REG   253,1    174576  328674 /usr/lib64/libtinfo.so.5.9
bash    2811  devl  mem   REG   253,1    163400  328378 /usr/lib64/ld-2.17.so
bash    2811  devl    0u  CHR   136,0       0t0       3 /dev/pts/0
bash    2811  devl    1u  CHR   136,0       0t0       3 /dev/pts/0
bash    2811  devl    2u  CHR   136,0       0t0       3 /dev/pts/0
bash    2811  devl  255u  CHR   136,0       0t0       3 /dev/pts/0
bash    6931  devl  cwd   DIR   253,1      4096   81927 /home/devl
bash    6931  devl  rtd   DIR   253,1      4096       2 /
bash    6931  devl  txt   REG   253,1    964608  327823 /usr/bin/bash

输出各列信息的意义如下:
COMMAND:进程的名称
PID:进程标识符
USER:进程所有者
FD:文件描述符,应用程序通过文件描述符识别该文件,如cwd、txt等
TYPE:文件类型,如DIR、REG等
DEVICE:指定磁盘的名称
SIZE:文件的大小
NODE:索引节点(文件在磁盘上的标识)
NAME:打开文件的确切名称

FD 列解析:
(1)cwd:表示 current work dirctory,即:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改
(2)txt:该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序
(3)lnn: library references (AIX)
(4)er: FD information error (see NAME column)
(5)jld: jail directory(FreeBSD)
(6)ltx: shared library text(code and data)
(7)mxx: hex memory-mapped type number xx.
(8)m86:DOS Merge mapped file
(9) mem: memory-mapped file
(10)mmap: memory-mapped device
(11)pd: parent directory
(12)rtd: root directory
(13)tr: kernel trace file (OpenBSD)
(14)v86 VP/ix mapped file
(15)0:表示标准输出
(16)1:表示标准输入
(17)2:表示标准错误
一般在标准输出、标准错误、标准输入后还跟着文件状态模式:r w u 等
(1)u:表示该文件被打开并处于读取/写入模式
(2)r:表示该文件被打开并处于只读模式
(3)w:表示该文件被打开并处于只写入模式
(4)空格:表示该文件的状态模式为 unknown ,且没有锁定
(5)- : 表示该文件的状态模式为 unknown ,且被锁定
同时在文件状态模式后面,还跟着相关的锁
(1)N:for a Solaris NFS lock of unknown type
(2)r: for a read lock on part of the file
(3)R:for a read lock on the entire file
(4)w: for a write lock on part of the file (文件的部分写锁)
(5)W: for a write lock on the entire file(整个文件的写锁)
(6)u: for a read and write lock of any length
(7)U: for a lock of unknown type
(8)x: for an SCO OpenServer Xenix lock on part of the file
(9)X:for an SCO OpentServer Xenix lock on the entire file
(10)space : if there is no lock

Type列解析:
文件和目录分别称为 REG 和 DIR。
CHR 和 BLK,分别表示字符和块设备;
UNIX、FIFO 和 IPv4,分别表示 UNIX 域套接字、先进先出 (FIFO) 队列和网际协议 (IP) 套接字。

参考:
https://zhuanlan.zhihu.com/p/68751873
https://www.cnblogs.com/aaronax/p/5654652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值