查看磁盘与目录的容量:ls df du
watch -- 监测命令的运行结果

命令: fuser与lsof

simplyzhao posted @ 2009年1月05日 05:59 in Commands & Tips with tags fuser lsof , 4305 阅读
Command: fuser
标识使用文件或套接字的进程
例如: fuser /home/simplyzhao
进程ID后的字母表示占用资源的方式,有下面几种表示:

c 当前路径(current directory.)我的理解是表示这个资源的占用是以文件目录方式,也就是进进入了需要释放的资源的路径,这是最常用的资源占用方式。

e 正在运行可执行文件(executable being run.),比如运行了光盘上的某个程序

f 打开文件( open file),缺省模式下f忽略。所以上面的例子中,虽然是开打了光盘上的Autorun.inf文件,但是给出的标识是c,而不是f。

r root目录(root directory).没有明白什么意思,难道是说进入了/root这个特定目录?

m mmap文件或者共享库( mmap’ed file or shared library).这应该是说某个进程使用了你要释放的资源的某个共享文件。

在查找的同时,你还可定指定一些参数,比如

-k 杀死这些正在访问这些文件的进程。除非使用-signal修改信号,否则将发送SIGKILL信号。

Command: lsof
lsof的功能很多,特別提醒大家, -c,-g,-p,-u,这四个参数最有用。更详细的资料请参看:man lsof。

1、查看文件系统阻塞

   根据工作需要,系统管理员想卸载一个文件系统并执行umount /mountpoint,但程序报告常常显示:umount: /mountpoint: device is busy;这是因为该文件系统上有正在打开的文件而不允许你这么做。这时,我们需要知道哪些文件、程序及用户仍在使用该系统,以便通知用户退出该系统,可 以使用lsof识别正在打开一个特定文件系统的进程,执行如下命令:
/usr/sbin/lsof /mountpoint
在这里,mountpoint就是安装位置。例如:
# /usr/sbin /lsof /home
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash12134 meng cwdDIR8,5 4096 32705 /home/meng
telnet 12176 meng cwdDIR8,5 4096 32705 /home/meng
bash19809 meng cwdDIR8,5 4096 32705 /home/meng
bash20276 meng cwdDIR8,5 4096 32705 /home/meng
su 20315 root cwdDIR8,5 4096 32705 /home/meng
bash20316 root cwdDIR8,5 4096 32705 /home/meng
csh 20374 root cwdDIR8,5 4096 32705 /home/meng
lsof 20396 root cwdDIR8,5 4096 32705 /home/meng
lsof 20397 root cwdDIR8,5 4096 32705 /home/meng

  显然,所有使用这些被打开的文件的进程都需要在文件系统能够被卸载前被终止。管理员以root身份,kill掉占用这个文件系统的进程,解除文件系统阻塞。

  2、搜索打开的网络连接

  如果想搜索IP地址为10.645.64.23的远程连接主机的所有网络连接,可以执行如下命令:
/usr/sbin/lsof
–i@10.65.64.23可以打开系统中该远程知己所有打开的套接字。
# lsof
-i@10.65.64.23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
telnetd 6605 root0u inet 0x14813f00 0t0 TCP xpp3:telnet->linuxone:33143 (ESTABLISHED)
telnetd 6605 root1u inet 0x14813f00 0t0 TCP xpp3:telnet->linuxone:33143 (ESTABLISHED)
telnetd 6605 root2u inet 0x14813f00 0t0 TCP xpp3:telnet->linuxone:33143 (ESTABLISHED)

  3、寻找本地断开的打开文件

  用户经常遇到这种情况,当一个进程正在向一个文件写数据时,该文件的目录可能被移动。这就产生了一个非常大的问题。例如,用户可能发现正在向/data写数据,但是却看不到文件增大,LSOF这个工具可以找到这样的错误,例如:
/usr/sbin/lsof +L1,通常可以看到下面的信息:
# lsof +L1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINKNODE NAME
svrMgt_mi458 root 4r VREG 8,00 0 3418 / (/dev/rz0a)
yes 677 root 1w VREG 8,0 186523648 0 92888 / (/de v/rz0a)
# lsof +L1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
svrMgt_mi458 root 4r VREG 8,0 0 0 3418 / (/dev/rz0a)
yes 677 root1w VREG 8,0 2735882240 92888 / (/dev/rz0a)

  我们可以用kill -9 PID命令来结束PID显示的命令排除错误,释放空间。

我们还可以用-a选项来限制lsof报告单文件系统中的链接数量。例如,为了限制到/data部分的输出,可以输入:/usr/sbin/lsof –a +L1 /data

  4、搜索被程序打开的所有文件及打开的文件相关联进程

  如果想知道执行PID号为637的sendmail命令打开了哪些文件的话,可以执行lsof -p 637命令。输出的结果如下:

  # lsof -p 637
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sendmail 637 root cwd VDIR8,6 512 470400 /usr/var/spool/mqueue
sendmail 637 root txt VREG8,6 466944 9650 /usr (/dev/rz0g)
sendmail 637 root txt VREG8,0 139264 16016 /sbin/loader
sendmail 637 root txt VREG8,0 1663104 38402 /shlib/libc.so
sendmail 637 root0r VCHR2,2 0t0 9607 /dev/null
sendmail 637 root1w VCHR2,2 0t0 9607 /dev/null
sendmail 637 root2w VCHR2,2 0t0 9607 /dev/null
sendmail 637 root3u unix 0x0c2fc280 0t0->0x1ead2b40
sendmail 637 root4u inet 0x0c34c200 0t0TCP *:smtp (LISTEN)

  上述输出信息显示了该程序当前打开的所有文件、设备、库及套接字等。

  执行下面的命令可以发现哪些进程正在使用某个特定的文件,如下所示,可以看出,只有系统记录后台进程syslogd打开messages这个文件。

  # lsof /var/adm/messages
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
syslogd 147 root 16w VREG8,6 2653365 22501 /usr/var/adm/messages

  5、其它使用命令(更详细的资料请man lsof,这部分参看了一些资料给大家总结一下)
若沒有加上任何的参数,lsof 会列出所有被程序打开的文件。
参数可以相互结合,ex: -a -b -c 等同于 -abc
-? -h 这两个参数意思相同,显示出 lsof 的使用说明。

  -a 参数被视为 AND (注意:-a参数一但加上 ,会影响全部的参数。)

  -c c 显示出以字母 c开头进程现在打开的文件
例:显示以init进程现在打开的文件
# lsof -c init
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
init 1 root cwd VDIR 4095,365376 8192 2 /
init 1 root txt VREG 4095,365376 286720 463 /sbin/init

  +d s 依照文件夹s来搜寻,此参数将不会继续深入搜寻此文件夹
例:显示在/usr/users/tongxl目录下被程序正在打开的文件(如下所示)
# lsof +d /usr/users/tongxl
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ksh 26946 root cwd VDIR8,6 512 51281 /usr/users/tongxl/c
a.out 26953 root cwd VDIR8,6 512 51281 /usr/users/tongxl/c

  +D D 同上,但是会搜索目录下的目录,时间较长。(注意︰lsof以此参数进行时,须花费较多的动态记忆体。尤其在处理较大的文件夹时,请务必审慎使用之。)
例:显示在/usr/local/文件夹下被程序正在打开的文件(如下)很明显可以看出二者的差别
# lsof +D /usr/users/tongxl
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ksh 26946 root cwd VDIR8,6 512 51281 /usr/users/tongxl/c
a.out 26953 root cwd VDIR8,6 512 51281 /usr/users/tongxl/c
a.out 26953 root txt VREG8,624576 51311 /usr/users/tongxl/c/a.out

  -d s 此参数以file descriptor (FD)值显示结果,可以采用范围表示,如 1-3 或 3-10 但 最前面的数一定要比最后面的数小。
举例:以FD为4显示
# lsof -d 4
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
syslogd 147 root4u inet 0x1fe0b980 0t0UDP *:syslog
binlogd 151 root4u inet 0x1fe0bd40 0t0UDP *:*
portmap 319 root4u inet 0x1fe0b740 0t0UDP *:111
mountd321 root4u VREG8,6 253 22516 /usr (/dev/rz0g)
nfsd 323 root4u inet 0x0c349e00 0t0TCP *:2049 (LISTEN)
rpc.statd 330 root4u inet 0x1ab42000 0t0TCP xpp3:1024 (LISTEN)
rpc.lockd 332 root4u inet 0x1fe0bbc0 0t0UDP xpp3:1028
snmpd 449 root4u unix 0x1aaf6500 0t0/var/esnmp/esnmpd
svrMgt_mi 457 root4r VREG8,00 3424 / (/dev/rz0a)
os_mibs 458 root4u inet 0x1ab475c0 0t0UDP *:*
cpq_mibs 460 root4u unix 0x1aaf77c0 0t0/var/esnmp/esnmp_sub460
advfsd472 root4u inet 0x0c320000 0t0TCP *:AdvFS (LISTEN)
insightd 475 root4r VDIR8,6 512 25610 /usr (/dev/rz0g)
inetd 506 root4u inet 0x1ab26700 0t0TCP *:ftp (LISTEN)
lpd 567 root4wW VREG8,64 451219 /usr (/dev/rz0g)
dtlogin 605 root4w VREG8,64 344028 /usr (/dev/rz0g)
Xdec 616 root4w VREG8,64 344028 /usr (/dev/rz0g)
sendmail 702 root4u inet 0x0c321900 0t0TCP *:smtp (LISTEN)
dtlogin 891 root4w VREG8,64 344028 /usr (/dev/rz0g)
dxconsole 907 root4w VREG8,64 344028 /usr (/dev/rz0g)
dtgreet 908 root4w VREG8,64 344028 /usr (/dev/rz0g)

  -g [s] 以程序的PGID (process group IDentification)显示,也可以采用范围(1-3)或个别(3,5)表示,若没有特别指定,则显示全部。
举例:以PGID为3显示
# lsof -g 3
COMMAND PID PGID USER FD TYPE DEVICE SIZE/OFF NODE NAME
kloadsrv 33 root cwd VDIR8,0 2560 2 /
kloadsrv 33 root txt VREG8,0 221184 16041 /sbin/kloadsrv
kloadsrv 33 root0r VCHR0,0 0t0 9608 /dev/console
kloadsrv 33 root1w VCHR0,0 0t0 9608 /dev/console
kloadsrv 33 root2w VCHR0,0 0t0 9608 /dev/console

  -i [i] 用以监听有关的任何符合的位址。若没有相关位置被指定,则监听全部。
语法: lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 --> IPv4 or IPv6
protocol --> TCP or UDP
hostname --> Internet host name
hostaddr --> IPv4位置
service --> /etc/service中的 service name (可以不只一个)
port --> 埠号 (可以不只一个)

  # lsof -i tcp@xp001

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
telnetd 26862 root0u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26862 root1u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26862 root2u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26986 root0u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)
telnetd 26986 root1u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)
telnetd 26986 root2u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)

  -l此参数禁止将user ID转换为登入名称。(预设显示登入名称)

  # lsof -l|more
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
kernel0 0 cwd VDIR8,02560 2 /
init 1 0 cwd VDIR8,02560 2 /
init 1 0 txt VREG8,0 286720 16015 / (/dev/rz0a)
kloadsrv 3 0 cwd VDIR8,02560 2 /
kloadsrv 3 0 txt VREG8,0 221184 16041 /sbin/kloadsrv
kloadsrv 3 0 0r VCHR0,0 0t0 9608 /dev/console
kloadsrv 3 0 1w VCHR0,0 0t0 9608 /dev/console
kloadsrv 3 0 2w VCHR0,0 0t0 9608 /dev/console

  +|-L [l] +或-表示正在打开或取消显示文件连结数. 若只有单纯的+L,后面没有任何数字,则表示显示全部。若其后有加上数字,只有文件连结数少于该数字的会被列出。

  -n不将IP位址转换成hostname,预设是不加上-n参数。
举例: lsof -i
tcp@xp001 -n
(您可以和上两张图比较一下,原先的hostname便回ip位置了)
# lsof -i
tcp@xp001 -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
telnetd 26862 root0u inet 0x0c349000 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3807 (ESTABLISHED)
telnetd 26862 root1u inet 0x0c349000 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3807 (ESTABLISHED)
telnetd 26862 root2u inet 0x0c349000 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3807 (ESTABLISHED)
telnetd 26986 root0u inet 0x1ab27100 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3988 (ESTABLISHED)
telnetd 26986 root1u inet 0x1ab27100 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3988 (ESTABLISHED)
telnetd 26986 root2u inet 0x1ab27100 0t0 TCP 10.65.69.147:telnet->10.65.69.131:3988 (ESTABLISHED)
# lsof -i
tcp@xp001
  COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
telnetd 26862 root0u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26862 root1u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26862 root2u inet 0x0c349000 0t0 TCP xpp3:telnet->xp001:3807 (ESTABLISHED)
telnetd 26986 root0u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)
telnetd 26986 root1u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)
telnetd 26986 root2u inet 0x1ab27100 0t0 TCP xpp3:telnet->xp001:3988 (ESTABLISHED)

  -s列出文件的大小,若该文件没有大小,则留下空白。

# lsof -s
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
kernel0 root cwd VDIR8,0 2560 2 /
init 1 root cwd VDIR8,0 2560 2 /
init 1 root txt VREG8,0 286720 16015 / (/dev/rz0a)
kloadsrv 3 root cwd VDIR8,0 2560 2 /
kloadsrv 3 root txt VREG8,0 221184 16041 /sbin/kloadsrv
kloadsrv 3 root0r VCHR0,09608 /dev/console
kloadsrv 3 root1w VCHR0,09608 /dev/console
kloadsrv 3 root2w VCHR0,09608 /dev/console

  -u s 以login name(登入名称)或UID,列出所正在打开文件。

# lsof -u tongxl
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
csh 26939 tongxl cwd VDIR8,6 1024 243236 /usr -- tongxl
csh 26939 tongxl txt VREG8,6 253952 12856 /usr (/dev/rz0g)
csh 26939 tongxl txt VREG8,0 139264 16016 /sbin/loader
csh 26939 tongxl txt VREG8,0 1663104 38402 /shlib/libc.so
csh 26939 tongxl0r VCHR1,0 0t0 9612 /dev/tty
csh 26939 tongxl 15u VCHR6,20t328 9618 /dev/pts/2
csh 26939 tongxl 16u VCHR6,20t328 9618 /dev/pts/2
csh 26939 tongxl 17u VCHR6,20t328 9618 /dev/pts/2
csh 26939 tongxl 18u VCHR6,20t328 9618 /dev/pts/2
csh 26939 tongxl 19u VCHR6,20t328 9618 /dev/pts/2
csh 26990 tongxl cwd VDIR8,6 1024 243236 /usr -- tongxl
csh 26990 tongxl txt VREG8,6 253952 12856 /usr (/dev/rz0g)
csh 26990 tongxl txt VREG8,0 139264 16016 /sbin/loader
csh 26990 tongxl txt VREG8,0 1663104 38402 /shlib/libc.so
csh 26990 tongxl0r VCHR1,0 0t0 9612 /dev/tty
csh 26990 tongxl 15u VCHR6,1 0t147797 9616 /dev/pts/1
csh 26990 tongxl 16u VCHR6,1 0t147797 9616 /dev/pts/1
csh 26990 tongxl 17u VCHR6,1 0t147797 9616 /dev/pts/1
csh 26990 tongxl 18u VCHR6,1 0t147797 9616 /dev/pts/1
csh 26990 tongxl 19u VCHR6,1 0t147797 9616 /dev/pts/1

提示“无法卸载卷(分区)”时的对策

背景:
在Linux下,有时会遇到(莫名奇妙的)无法正常卸载文件系统的问题,比如U盘。相对Windows下的束手无策或者使用第三方软件,Linux下使用一个命令即可解决问题。下面介绍怎样安全卸载文件系统,假设出现了下面的异常(黑体表示要输入的指令,下同):
sudo umount /media/disk
umount: /media/disk: device is busy

方法1:使用fuser
可以使用fuser命令寻找挂载在/media/disk的文件系统所关联的进程:
/sbin/fuser -m /media/disk
/media/music: 2650c 2720c
返回值表示有两个进程在访问此挂载点,PID分别为2650、2720,并且访问方式都是将此目录当作进程的当前工作目录。

此时,可以查看第一个进程是什么(在此只是示例,gnome-session才不会访问那么奇怪的地方呢^_^):
ps -e |grep 2650
2650 ? 00:00:01 gnome-session
可以使用pkill gnome-session或者kill 2650来结束此进程,另外一个进程也可以如法炮制。
到此,就可以再次尝试使用umount命令卸载/media/disk了!

fuser还提供了一个便捷的参数,使用下面命令可以立即杀死文件系统关联的所有进程:
/sbin/fuser -k /media/disk

方法2:使用lsof
fuser返回的PID虽然在末尾有个状态指示符,但对于了解进程到底正在做什么远远不够。这时可以使用lsof命令,返回在指定文件系统上有打开文件的进程的详细信息:

/usr/sbin/lsof /DATA
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
gnome-ses 2650 youran cwd DIR 8,5 4096 26771457 /DATA/youran
python 2803 youran cwd DIR 8,5 4096 26771457 /DATA/youran
escd 2810 youran 3w REG 8,5 1254 557183 /DATA/youran/.redhat/esc/esc.log
pam-panel 2811 youran cwd DIR 8,5 4096 26771457 /DATA/youran
evolution 2822 youran 23u REG 8,5 12288 2885286 /DATA/youran/.evolution/addressbook/local/system/addressbook.db
evolution 2822 youran 24r REG 8,5 118 2885287 /DATA/youran/.evolution/addressbook/local/system/addressbook.db.summary
scim-pane 2859 youran mem REG 8,5 10044356 2885529 /DATA/youran/.fonts/simhei.ttf


上面显示了当前在访问/DATA文件系统的进程信息。输出因太多,被截短。这时,可以方便地使用grep命令找出是什么进程在阻碍文件系统的卸载,并且能看到它现在在干什么。例如:

/usr/sbin/lsof /DATA |grep /DATA/youran/.evolution/
evolution 2822 youran 23u REG 8,5 12288 2885286 /DATA/youran/.evolution/addressbook/local/system/addressbook.db
evolution 2822 youran 24r REG 8,5 118 2885287 /DATA/youran/.evolution/addressbook/local/system/addressbook.db.summary

上面的输出显示当前正在访问/DATA/youran/.evolution子目录的进程,以及打开的文件。同样可以使用pkill或者kill结束这些进程。

方法3:umount -l
新版本的umount提供了-l参数,以“懒惰”的方式立即卸载文件系统,然后试图清除进程对文件系统的引用:
umount -l /media/disk
可以一试。
来源:
http://tonykorn97.itpub.net/post/6414/130087
http://bbs.unixtech.cn/read.php?tid=1454&page=e&fpage=1
Enjoy Linux, Fighting.
Avatar_small
maid services abu dh 说:
2019年9月19日 22:16

Cleaning up service franchise owners will be entrepreneurs who seem to work in the names with franchise providers. They may specialise in cleaning dwellings or private buildings. Anyway, they clean, pick right up trash, cleanse restrooms and also bathrooms plus shampoo brown area rug.

Avatar_small
part time maids in d 说:
2020年7月01日 16:22

vital point that you not forget to bear in mind is a insurance variable. Make sure the man or women or a maid company you assume for choosing is insured properly. This is important if the human being gets injured at home or receives any specific injury, it is likely you will face quite a few problems for instance high health bills. Just as before, if a strong uninsured cleaning service damages your stuff, you are expected to face significantly of challenge in claiming for your personal damages. This becomes even more difficult when your employee's points are unknown back.

Avatar_small
painting services in 说:
2020年7月01日 16:22

Whether that you're self-employed or a worker, you might spend many of your workday alone or as a minimum outside to your supervisor's realize. You should work appropriately and good without point supervision. Good electrician for office should provide complete estimates of that time period required to undertake a endeavor. He has most of the skills required by just a good electrician.

Avatar_small
easterniowanews 说:
2021年3月23日 00:14

This sort of news is critical especially through the months wherein hurricane, typhoon as well as other natural calamities are anticipated. Latest weather conditions news are usually what a lot of people want you just read to make certain their safety also to know what exactly is happening inside their country or perhaps locality.

Avatar_small
dubai maid service 说:
2021年6月14日 21:49

Meant for twice thirty days, we is capable of a comprehensive house, deep cleaning on your behalf. Hire some of our Professional Full cleaning products in Dubai together with Sharjah and get you to living real estate more nice and clean and tempting. Get Low priced Deep maintenance services with Cit Help a trusted Cleaning provider in Dubai


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter