lsof 查找进程打开的文件 查找打开文件的进程 查找占用端口的进程
- 2015-07-25 22:08:00
- admin
- 原创 2061
一、lsof详解
lsof用于列出系统打开的文件,lsof需要访问内核信息,所以root权限查看最有效。
FD是文件描述符:cwd是当前进程工作目录,txt是二进制代码,mem是内存映射文件,数字是打开的文件描述符;
TYPE是文件类型:DIR表示目录,REG表示文件,CHR表示字符设备,IPv4和IPv6表示IP套接字,unix表示unix套接字;
NODE是索引节点:IP套接字的索引节点是TCP;
列出系统所有打开的文件数量:
lsof | wc -l
已经删除但仍被进程持有的文件:
lsof | grep deleted
根据PID查找进程打开的文件:
lsof -p 6268
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tail 6268 root cwd DIR 252,1 4096 393217 /root
tail 6268 root rtd DIR 252,1 4096 2 /
tail 6268 root txt REG 252,1 59696 1181536 /usr/bin/tail
tail 6268 root mem REG 252,1 157032 524337 /lib64/ld-2.12.so
tail 6268 root mem REG 252,1 1926760 524357 /lib64/libc-2.12.so
tail 6268 root mem REG 252,1 99154480 1192343 /usr/lib/locale/locale-archive
tail 6268 root 0u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 1u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 2u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 3r REG 252,1 128 393266 /root/main.cpp
tail 6268 root 4r DIR 0,10 0 1 inotify
根据命令前缀查找进程打开的文件:
lsof -c tail
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tail 6268 root cwd DIR 252,1 4096 393217 /root
tail 6268 root rtd DIR 252,1 4096 2 /
tail 6268 root txt REG 252,1 59696 1181536 /usr/bin/tail
tail 6268 root mem REG 252,1 157032 524337 /lib64/ld-2.12.so
tail 6268 root mem REG 252,1 1926760 524357 /lib64/libc-2.12.so
tail 6268 root mem REG 252,1 99154480 1192343 /usr/lib/locale/locale-archive
tail 6268 root 0u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 1u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 2u CHR 136,1 0t0 4 /dev/pts/1
tail 6268 root 3r REG 252,1 128 393266 /root/main.cpp
tail 6268 root 4r DIR 0,10 0 1 inotify
查找打开文件的进程:
lsof myfile
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tail 6268 root 3r REG 252,1 128 393266 myfile
查找占用端口的进程:
lsof -i [protocol][@hostname|hostaddr][:service|port]
lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 24501 root 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25622 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25623 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25624 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25625 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25626 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25627 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25628 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 25629 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 26448 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
httpd 26449 apache 4u IPv6 4351042 0t0 TCP *:http (LISTEN)
二、C++程序打开同一个文件20次
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main ()
{
FILE * pFile;
int cnt = 20;
for (int idx=1; idx<=cnt; ++idx)
{
printf("idx %d\n", idx);
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
printf("fildid %08x\n", pFile);
else
printf("%s\n", strerror(errno));
}
sleep(10 * 60);
return 0;
}
lsof -p 3726
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
a.out 3726 root cwd DIR 252,1 4096 393217 /root
a.out 3726 root rtd DIR 252,1 4096 2 /
a.out 3726 root txt REG 252,1 7577 406966 /root/a.out
a.out 3726 root mem REG 252,1 157072 524337 /lib64/ld-2.12.so
a.out 3726 root mem REG 252,1 1926520 524376 /lib64/libc-2.12.so
a.out 3726 root mem REG 252,1 599392 524528 /lib64/libm-2.12.so
a.out 3726 root mem REG 252,1 989840 1184609 /usr/lib64/libstdc++.so.6.0.13
a.out 3726 root mem REG 252,1 93320 524642 /lib64/libgcc_s-4.4.7-20120601.so.1
a.out 3726 root 0u CHR 136,1 0t0 4 /dev/pts/1
a.out 3726 root 1u CHR 136,1 0t0 4 /dev/pts/1
a.out 3726 root 2u CHR 136,1 0t0 4 /dev/pts/1
a.out 3726 root 3w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 4w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 5w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 6w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 7w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 8w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 9w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 10w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 11w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 12w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 13w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 14w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 15w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 16w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 17w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 18w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 19w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 20w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 21w REG 252,1 0 407318 /root/myfile.txt
a.out 3726 root 22w REG 252,1 0 407318 /root/myfile.txt