简体   繁体   English

Linux中“ lsof”命令的替代方法

[英]Alternative to “lsof” command in Linux

I need a command in Linux(SUSE & RHEL) to find the list of open files as "lsof" command is taking time to give output.Even "lsof -n" command also takes a lot of time. 我需要在Linux中使用命令(SUSE&RHEL)来查找打开文件的列表,因为“ lsof”命令要花一些时间才能输出。即使“ lsof -n”命令也要花费很多时间。 Do we have any alternative command to "lsof"? 我们是否可以使用其他命令代替“ lsof”?

Thanks, Sanghamitra 谢谢,Sanghamitra

You can read the open files from the proc file system. 您可以从proc文件系统中读取打开的文件。

for p in /proc/{0..9}*; do
  i=$(basename "$p")
  for f in "$p"/fd/*; do
    l=$(readlink -e "$f")
    if [ "$l" ]; then
      echo "$i: $l"
    fi
  done
done | sort -u | sort -n

In short, use lsof -n . 简而言之,请使用lsof -n


lsof waits a lot because lsof等待很多,因为

  1. It uses a lot of time to scan all the devices in /dev 它花费大量时间来扫描/dev所有设备
  2. It uses also a lot of time to get a reverse dns query from all the endpoints of the network sockets it found. 它也花费大量时间从发现的网络套接字的所有端点获取反向dns查询。 Many of them doesn't have a valid revdns (like 0.0.0.0 ), and querying for them results timeout. 其中许多没有有效的revdns(如0.0.0.0 ),并且查询结果超时。
  3. It does it as a single-threaded, not event-oriented app. 它作为单线程而不是面向事件的应用程序来执行。

You can give it a significant acceleration by disabling the dns resolution part with a -n . 通过使用-n禁用dns解析部分,可以大大提高它的速度。

Anyways, extending @ceving 's answer, you could also watch not only the list of the opened file descriptors, but also the list of the files mapped into the address space of the processes ( cat /proc/<pid>/map ). 无论如何,扩展@ceving的答案,您不仅可以查看打开的文件描述符的列表,还可以查看映射到进程地址空间( cat /proc/<pid>/map )的文件的列表。


Typically, if you have a process running surprisingly slowly, a 通常,如果您的进程运行异常缓慢,

strace -f -tt -o sux <your command line>

will dump into the file sux the kernel calls what the process did, with timestamps. 它将使用时间戳将内核执行的操作转储到文件sux In essence, so you will be able to track, what exactly this process did and how long . 从本质上讲,您将能够跟踪到此过程的确切执行时间以及持续时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM