Linux lsof tool learning notes

Source: Internet
Author: User

1. What is lsof?

Lsof is a tool in Linux used to list files opened in the current system. These files include regular files, directories, pipelines, character devices, Block devices, and unix sockets.

2. What is the role of lsof?

(1) Have you ever encountered an open File Failure in your program? Then we can use lsof to check the number of open files in the system. Will it be related to this.

From the following results, you can see that the maximum number of open files preset by the system is 1024, so if you have opened 1024 files, the open will fail.

Usually this problem occurs because open is called in the program, but it is not closed after the file is used up.

Sh-# ulimit-
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 File (-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

In this experiment, we intentionally changed the number of open files to 1. You will find that many linux commands are no longer running.

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) From the execution result of lsof, we can also see which users are using the current file system, or which files have been opened in the current 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 ))

When unmount a file system fails, you can use lsof to check whether your file system still has open files.

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

It can be seen that a test.txt file is open, so you need to close the file and then unmount it to succeed.

(3) Sometimes we can use lsof to restore files that have been deleted (for example, deleted by mistake). Why?

This is because these open files have corresponding images in RAM, so we can use the special file system/proc to restore them. The specific steps are as follows:

A. Use lsof to check which process opened the file and the file descriptor of the opened file:
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. Next you will find that this is the content of the file you deleted earlier:

Sh-# cat/proc/747/fd/46
Dfdfd

C. Save the accidentally deleted file with caution:

Sh-3.2 # cat/proc/537/fd/46>/mnt/usb/sda1/restore.txt
Sh-3.2 # cat/mnt/usb/sda1/restore.txt
Dfdfd

The above are some of my experiences and applications in the learning process. In fact, the tool lsof is very powerful.

If there are new applications later, we will share them with you. If you have any questions, please feel free to discuss them. Thank you.

Linux lsof commands

Linux lsof command usage

Lsof --- a powerful tool for Linux to View File Information

Use lsof to restore accidentally deleted files

Linux Command lsof to view the current system IO status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.