Linux lsof 命令

检测端口未被占用

1
lsof -i:<port>

指定进程号,可以查看该进程打开的文件

1
lsof -p <pid>

查询所有进程的 open files 数量

1
2
3
4
5
6
7
8
9
10
11
psof_file=~/psof.txt
# 每次执行前,清空 psof_file 文件内容
echo "" > $psof_file
# $(ps -eo pid | grep -v PID) 获取所有进程 id
for pid in $(ps -eo pid | grep -v PID)
# 查询进程的 open files 数量
do echo $pid":"$(lsof -p $pid|wc -l) >> $psof_file;
# do echo $pid":"$(lsof -n|grep $pid|wc -l) >> $psof_file;
done
# 对结果进行排序
sort -nrk 2 -t: $psof_file

统计各进程打开句柄数:lsof -n|awk ‘{print $2}’|sort|uniq -c|sort -nr

统计各用户打开句柄数:lsof -n|awk ‘{print $3}’|sort|uniq -c|sort -nr

统计各命令打开句柄数:lsof -n|awk ‘{print $1}’|sort|uniq -c|sort -nr

第一列是打开的句柄数,第二列是进程ID。

查看打开句柄总数
lsof -n|awk ‘{print $2}’|wc -l
查看系统中进程占用的句柄数,根据打开文件句柄的数量降序排列,其中第二列为进程ID:
lsof -n|awk ‘{print $2}’|sort|uniq -c|sort -nr|more
查询系统中指定进程占用的句柄数
lsof -n | grep [PID]| wc -l

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@grg-zhtest1 ~]# lsof -h
lsof 4.87
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c ^c /c/[bix] +c w COMMAND width (9) +d s dir s files
-d s select by FD set +D D dir D tree *SLOW?* +|-e s exempt s *RISKY*
-i select IPv[46] files -K list tasKs (threads) -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O no overhead *RISKY* -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -X skip TCP&UDP* files -Z Z context [Z]
-- end option scan
+f|-f +filesystem or -file names +|-f[gG] flaGs
-F [f] select fields; -F? for help
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
+m [m] use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s exclude(^)|select PIDs -S [t] t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g [s] exclude(^)|select and print process group IDs
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
+|-r [t[m<fmt>]] repeat every t seconds (15); + until no files, - forever.
An optional suffix to t is m<fmt>; m must separate t from <fmt> and
<fmt> is an strftime(3) format for the marker line.
-s p:s exclude(^)|select protocol (p = TCP|UDP) states by name(s).
-u s exclude(^)|select login|UID set s
-x [fl] cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/fc091577.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!