Linux lsof tool學習筆記

來源:互聯網
上載者:User

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
dfdfd

c. 將這個誤刪的檔案小心翼翼的存回去:

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 命令詳解

Linux lsof命令使用詳解

lsof---Linux查看檔案資訊的強大工具

用lsof恢複誤刪的檔案

Linux命令lsof查看當前系統的IO情況

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.