fuser与lsof -- 查看文件被哪些进程占用

命令安装

[root@template ~]# yum -y install psmisc
[root@template ~]# rpm -ql psmisc
/usr/bin/killall
/usr/bin/peekfd
/usr/bin/prtstat
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/sbin/fuser

fuser

  • Introduction

    • 查看文件、目录或者socket所属进程的PID,由此知道该文件或目录被哪些进程所使用
  • Usage

[root@template ~]# man fuser
  fuser [options] [name]
    # -a:显示所有指定文件的情况,即使没有进程访问它们
    # -k:找出使用该文件或目录的进程PID,并kill,使用的信号是SIGKILL
    # -i:一般和-k结合使用,kill之前询问
    # -signal:发送信号,如果不指定,默认-9
    # -s:静默模式。不能与-a一起使用
    # -m:指定已挂载的文件系统或者块设备。所有访问该文件系统的进程都会被列出来。
    # -v:详细信息。
    # 输出结果中PID后的标识符有以下几种:
        # c:代表当前目录
        # e:代表文件为可执行程序
        # f:被打开的文件或者目录,默认不显示
        # F:被打开且正在写入的文件或目录,默认不显示
        # r:根目录
        # m:映射文件或共享库
  • Example
# 查看使用crond文件的进程
[root@template ~]# fuser /usr/sbin/crond 
/usr/sbin/crond:       567e
[root@template ~]# ps -ef | grep [5]67
root        567      1  0 Aug04 ?        00:00:00 /usr/sbin/crond -n
# 查出所有使用sshd文件的进程并尝试kill
[root@template ~]# fuser -ki /usr/sbin/sshd
/usr/sbin/sshd:      14991e 14992e 15099e
Kill process 14991 ? (y/N) 
# 查看是用/dev/sda3块设备的所有进程
[root@template ~]# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        38G  1.2G   37G   3% /
[root@template ~]# fuser -m /dev/sda3
/dev/sda3:               1rce     2rc     3rc     5rc....
# 查看哪些程序在使用80端口
[root@template ~]# fuser -v 80/tcp
                     USER        PID ACCESS COMMAND
80/tcp:              root      15347 F.... nginx
                     nginx     15348 F.... nginx

lsof

  • Introduction

    • 查看进程打开了哪些文件,当然也可以查看文件被哪些进程打开。不加参数时列出所有被打开的文件。
  • Usage

[root@template ~]# man lsof
  fuser [options] [name]
    # 命令后直接跟文件名:显示打开指定文件的所有进程列表,一般配合grep使用
    # -c string:显示在“COMMAND”列中包含指定string的进程所打开的文件,该选项可以多次使用
    # -p PID:查看该PID对应的进程打开了哪些文件
    # -a:and的意思,组合多个参数一起使用
    # -i:显示符合条件的进程情况
    # -n:不反解ip为hostname
[root@template ~]# lsof | head -3
COMMAND     PID  TID    USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd       1         root  cwd       DIR                8,3       238         64 /
systemd       1         root  rtd       DIR                8,3       238         64 /
    # COMMAND:进程名称
    # PID:进程表示ID
    # USER:进程所有者
    # FD:文件描述符,程序通过文件描述符来识别该文件
    # TYPE:文件类型
    # DEVICE:指定磁盘的名称
    # SIZE/OFF:文件大小或者文件偏移量
    # NODE:索引节点(文件在磁盘上的标识)
    # NAME:打开文件的确切名称
  • Example
# 列出哪些进程在使用该文件
[root@template ~]# lsof /usr/sbin/sshd
# 列出多个程序打开的文件信息
[root@template ~]# lsof -c crond -c nginx
# 列出某用户及某程序打开文件信息
[root@template ~]# lsof -u nginx -a -c nginx
# 列出某个PID对应进程打开的文件信息
[root@template ~]# lsof -p 1
# 列出所有网络连接
[root@template ~]# lsof -i
# 列出所有tcp网络连接
[root@template ~]# lsof -i tcp
# 列出谁在使用某个端口
[root@template ~]# lsof -i:22
# 列出某用户的所有活跃网络连接
[root@template ~]# lsof -u nginx -a -i
# 列出某个用户打开的文件信息
[root@template ~]# lsof -u nginx
# 查找某个目录下被打开的文件信息
[root@template ~]# lsof +d /var/log/nginx
# 递归查找某个目录中被打开的文件信息
[root@template ~]# lsof +D /var/log/nginx


原文地址:https://www.cnblogs.com/ccbloom/p/11301159.html