导读:
一、用事实说话
二、关于LSOF命令的其它用法:
三、参考文档:
正文:
lsof:Finding open files with lsof
作用:查看文件被哪些进程打开
【实验】Linux下文件删除、句柄与空间释放问题
窗口1:创建test文件,并用less命令打开它
[root@monitor monitor]# echo "zhengBin is Bkeep" >> test
[root@monitor monitor]# ls –sortk
8-rw-r--r—1root1 Dec916:02 error.txt
120-rw-r--r—1root 112Dec1016:38 test
[root@monitor monitor]# less test
zhengBin is Bkeep
(END)
窗口2:使用lsof命令排查问题
[root@monitor monitor]# rm–rf test
[root@monitor monitor]# lsof |grep test//注意,这里test文件已被标记为“deleted”,但仍然被less进程锁定(pid:23253)
less23253 root4rREG253,0182850936 /usr/monitor/test (deleted)
[root@monitor ~]# ps -ef |grep 23253//查看less进程的系统用户,为root!
root23253 230420 17:16 pts/100:00:00 less test
root23291 232560 17:17 pts/000:00:00 grep 23253
[root@monitor ~]# kill -9 23253//杀掉该进程,系统才会释放test占用的磁盘空间。(重启系统或服务也行)
[root@monitor ~]# lsof |grep test
没有了,说明空间已被释放!
二、关于lsof命令的其它用法:
1,查看目录和文件正在被那些进程使用。
[root@monitor monitor]# umount /
umount: /: device is busy
umount: /: device is busy
[root@monitor monitor]# lsof /
COMMANDPIDUSERFDTYPE DEVICESIZENODE NAME
init1rootcwdDIR253,040962 /
init1rootrtdDIR253,040962 /
init1roottxtREG253,038620 9306178 /sbin/init
init1rootmemREG253,0125736 6686192 /lib/ld-2.5.so
init1rootmemREG253,01602128 6686193 /lib/libc-2.5.so
init1rootmemREG253,016428 6686196 /lib/libdl-2.5.so
init1rootmemREG253,093508 6686210 /lib/libselinux.so.1
init1rootmemREG253,0242880 6686209 /lib/libsepol.so.1
migration2rootcwdDIR253,040962 /
migration2rootrtdDIR253,040962 /
ksoftirqd3rootcwdDIR253,040962 /
ksoftirqd3rootrtdDIR253,040962 /
2,查看远程ip调用了系统那些进程
[root@monitor monitor]# netstat -natp
tcp01008 ::ffff:192.168.254.51:22::ffff:192.168.254.149:1066 ESTABLISHED 22779/0
[root@monitor monitor]# lsof -i @192.168.254.149
COMMANDPID USERFDTYPE DEVICE SIZE NODE NAME
sshd22779 root3uIPv698877TCP 192.168.254.51:ssh->192.168.254.149:fpo-fns (ESTABLISHED)
3,根据端口号查看服务名
[root@monitor monitor]# lsof -i :25
COMMANDPID USERFDTYPE DEVICE SIZE NODE NAME
sendmail 4653 root4uIPv49307TCP monitor:smtp (LISTEN)
4,谁在打开sendmail.pid文件?
[root@monitor run]# lsof /var/run/sendmail.pid
COMMANDPID USERFDTYPE DEVICE SIZENODE NAME
sendmail 4653 root5wWREG253,033 9667079 /var/run/sendmail.pid
5,【重要】查看某个进程号所打开的所有系统文件
参数说明:
-a,所有参数都必须持有真实的文件显示
-p,进程号
-d,txt记录将被过滤掉;^排除的意思(the carat [^] means exclude).
[root@monitor run]# lsof -a -p 4653 -d ^txt//【重要】
COMMANDPID USERFDTYPEDEVICESIZENODE NAME
sendmail 4653 rootcwdDIR253,04096 9666678 /var/spool/mqueue
sendmail 4653 rootrtdDIR253,040962 /
sendmail 4653 rootmemREG253,06684758 /lib/libdb-4.3.so (path inode=6684892)
sendmail 4653 rootDELREG253,02107155 /usr/lib/liblber-2.3.so.0.2.15.#prelink#.1kBcGu
sendmail 4653 rootDELREG253,02107070 /usr/lib/libgssapi_krb5.so.2.2.#prelink#.Y8RNd8
sendmail 4653 rootmemREG253,02101464 /usr/lib/libz.so.1.2.3 (path inode=2117529)
sendmail 4653 rootmemREG253,046680 6684713 /lib/libnss_files-2.5.so
sendmail 4653 rootmemREG253,014596 2162994 /usr/lib/sasl2/libanonymous.so.2.0.22
sendmail 4653 rootmemREG253,0 905200 2164220 /usr/lib/sasl2/libsasldb.so.2.0.22
sendmail 4653 rootmemREG253,06684723 /lib/libresolv-2.5.so (path inode=6686208)
sendmail 4653 rootmemREG253,06684701 /lib/libcrypt-2.5.so (path inode=6686216)
sendmail 4653 rootmemREG253,06684759 /lib/libkeyutils-1.2.so (path inode=6686207)
sendmail 4653 root2wCHR1,31449 /dev/null
sendmail 4653 root3uunix 0xd45eb7409306 socket
sendmail 4653 root4uIPv49307TCP monitor:smtp (LISTEN)
sendmail 4653 root5wWREG253,033 9667079 /var/run/sendmail.pid
6,ls命令参数解释
[root@monitor monitor]# ls –sortk
8-rw-r--r—1root1 Dec916:02 error.txt
120-rw-r--r—1root 112Dec1016:38 test
参数说明:
s:第一列显示的块大小(个);o:类似l但不显示group;S:按文件大小排序
r:倒序;t:按时间排序k:字节数按KB来显示