Welcome 微信登录

首页 / 操作系统 / Linux / Linux lsof tool学习笔记

1. lsof是什么?lsof是Linux下面一个tool,用来列出当前系统中所打开的文件,这些文件包括正规文件、目录、管道、字符设备、块设备、unix socket等等。2. 那lsof有什么作用呢?(1)你的程序有没有出现过open文件失败的情况?那我们可以通过lsof来查看系统中已经open了多少文件,会不会跟这有关系。从以下的结果你可以看到系统预设的最大open files是1024个,所以如果你已经打开了1024个文件了,那就会open失败了。通常发生此问题的原因都是程序中调用了open,但是用完文件之后却没有去close。sh-# ulimit -a
core file size          (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
scheduling priority           (-e) 0
file size             (blocks, -f) unlimited
pending signals               (-i) 2293
max locked memory     (kbytes, -l) 64
max memory size       (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues   (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes              (-u) 2293
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited我们做个试验,故意将open files数目改为1,你会发现很多linux command已经无法运行了。sh-# ulimit -n 1
sh-# ls
ls: error while loading shared libraries: librt.so.1: cannot open shared object file: Error 24
sh-# busybox which
busybox: error while loading shared libraries: libm.so.6: cannot open shared object file: Error 24(2)从lsof的执行结果,我们也可以看到有哪些user正在使用当前的file system;或者是当前file system中有哪些已经被打开的文件。sh-# umount /mnt/sda1/
umount:  /mnt/usb/sda1: device is busy.
        (In some cases useful info about processes that use
       the device is found by lsof(8) or fuser(1))当unmount一个file system失败时,可以使用lsof来查看你的file system是否仍然有正在open着的文件。sh-# ./lsof /mnt/sda1/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ooxx 537 root 46r REG    8,1        6 45 /mnt/sda1/test.txt可以看到有一个test.txt是打开的,所以需要先close这个文件再去unmount才能成功。(3)有时候我们能够使用lsof来恢复那些已经删除掉(比如说误删)的档案,为什么能做到?这是因为这些open的文件在RAM中都有对应的映像,所以我们就可以用/proc这个特殊的file system来恢复,具体步骤是:a. 使用lsof查到哪个process打开了这个档案,以及这个被打开档案的file descriptor:
sh-3# ./lsof | grep test.txt
process_name    747      root 46r      REG        8,1        6    45 /mnt/sda1/test.txt (deleted)
process_name    747  541 root 46r      REG        8,1        6    45 /mnt/sda1/test.txt (deleted)b. 接下来你会发现这果然就是你之前删掉的文件的内容:sh-# cat /proc/747/fd/46
dfdfdc. 将这个误删的档案小心翼翼的存回去:sh-3.2# cat /proc/537/fd/46 > /mnt/usb/sda1/restore.txt
sh-3.2# cat /mnt/usb/sda1/restore.txt
dfdfd以上是个人学习过程中的一些心得和应用,实际上lsof这个tool的功能是非常非常的强大。后面如果有新的应用,会再分享出来;如果有任何问题,非常欢迎提出来讨论,谢谢。相关阅读:Linux lsof 命令详解 http://www.linuxidc.com/Linux/2013-03/80982.htmLinux lsof命令使用详解 http://www.linuxidc.com/Linux/2012-11/74868.htmlsof---Linux查看文件信息的强大工具 http://www.linuxidc.com/Linux/2012-02/54786.htm用lsof恢复误删的文件 http://www.linuxidc.com/Linux/2006-11/1022.htmLinux命令lsof查看当前系统的IO情况 http://www.linuxidc.com/Linux/2011-03/33216.htmLinux 命令整理Vim 多窗口编辑相关资讯      lsof 
  • 使用lsof恢复误删日志文件或数据库  (04/15/2015 19:55:20)
  • lsof 常用参数图解  (03/11/2014 10:14:51)
  • Linux 系统监控、诊断工具之 lsof  (08/19/2013 08:50:03)
  • Linux 命令教程:lsof 入门  (10/27/2014 17:31:59)
  • 10 个 lsof 命令行的使用示例  (03/02/2014 10:06:56)
  • Linux lsof命令使用详解  (11/26/2012 10:38:52)
本文评论 查看全部评论 (0)
表情: 姓名: 字数