fuser与lsof命令

fuser能识别出正在对某个文件或端口访问的进程
lsof能够找出正在对指定文件访问的进程,
那么它们两者之间有何区别?
fuser有一个特别的用法在于它可以一次杀死那些正在访问指定文件的进程

[root@cloud ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    36G  2.9G   31G   9% /
tmpfs          tmpfs  931M  228K  931M   1% /dev/shm
/dev/sda1      ext4   388M   31M  337M   9% /boot
/dev/sdb1      ext4    20G  223M   19G   2% /data
[root@cloud ~]# umount /data
umount: /data: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@cloud ~]# fuser -v /data/
                     用户     进程号 权限   命令
/data/:              root      73924 ..c.. bash
[root@cloud ~]# 

用fuser -k 杀死所有正在使用该文件的进程:

[root@cloud ~]# fuser -k /data
/data:               73924c
[root@cloud ~]# fuser -v /data
[root@cloud ~]# umount /data
[root@cloud ~]# df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    36G  2.9G   31G   9% /
tmpfs          tmpfs  931M  228K  931M   1% /dev/shm
/dev/sda1      ext4   388M   31M  337M   9% /boot
[root@cloud ~]# 

fuser -l 可以列出它所知的信号(高级用法):

[root@cloud ~]# fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED
[root@cloud ~]# 

高级用法我还没试过找天试了再分享

lsof也可以查看占用指定文件的程序:

[root@cloud ~]# lsof /data/
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash    73973 root  cwd    DIR    8,3     4096 1048577 /data
vim     73988 root  cwd    DIR    8,3     4096 1048577 /data
[root@cloud ~]# kill -9 73988
[root@cloud ~]# lsof /data/
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash    73973 root  cwd    DIR    8,3     4096 1048577 /data
[root@cloud ~]# kill -9 73973
[root@cloud ~]# lsof /data/
[root@cloud ~]# 

lsof 查看占用端口的程序,有时候进程启动时进程pid被占用,这时就可以用losf -i :端口号 看看了

[root@cloud ~]# lsof -i :22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    3133 root    3u  IPv4  66527      0t0  TCP *:ssh (LISTEN)
sshd    3133 root    4u  IPv6  66529      0t0  TCP *:ssh (LISTEN)
sshd    3885 root    3r  IPv4  87180      0t0  TCP 192.168.31.185:ssh->192.168.31.165:55461 (ESTABLISHED)
sshd    3889 root    3r  IPv4  87237      0t0  TCP 192.168.31.185:ssh->192.168.31.165:55464 (ESTABLISHED)
sshd    3935 root    3r  IPv4  87610      0t0  TCP 192.168.31.185:ssh->192.168.31.165:55466 (ESTABLISHED)
[root@cloud ~]# 
comments powered by Disqus