每天進步一點點- lsof 命令查詢指定使用者、程式、埠開啟的檔案
lsof 需要root許可權執行
先執行下面程式碼供測試:
public class TestSleep {
public static void main(String[] args) {
System.out.println("being sleep"+new Date().toLocaleString());
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("/home/boco/script/sleep.sh");
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<output></output>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
System.out.println("end sleep"+new Date().toLocaleString());
}
}
#!/bin/bash
#/home/boco/script/sleep.sh
echo "begin"
sleep 1000
echo "end"
1、查詢所有開啟的檔案
[root@redhat5 ~]# lsof | head COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME init 1 root cwd DIR 8,3 4096 2 / init 1 root rtd DIR 8,3 4096 2 / init 1 root txt REG 8,3 38652 4484999 /sbin/ini t init 1 root mem REG 8,3 125736 492620 /lib/ld-2 .5.so init 1 root mem REG 8,3 1611564 492621 /lib/libc -2.5.so init 1 root mem REG 8,3 16428 492622 /lib/libd l-2.5.so init 1 root mem REG 8,3 93508 492636 /lib/libs elinux.so.1 init 1 root mem REG 8,3 245376 492635 /lib/libs epol.so.1 init 1 root 10u FIFO 0,17 1506 /dev/init ctl
FD - File descriptor
FD 列包含這樣一些值
cwd - Current working directory txt - Text file mem - Memory Mapped file mmap - Memory Mapped device Number - It represent the actual file descriptor. For example, 0u, 1w and 3r r 是讀的意思,w 是寫,u 代表讀寫
Type 代表檔案型別,例如:
>REG - Regular file >DIR - Directory >CHR - Character special file >FIFO - First in first out
2、查詢使用者開啟的檔案
[root@redhat5 ~]# lsof -u boco COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME bash 3599 boco cwd DIR 8,3 4096 752939 /home/boco/script bash 3599 boco rtd DIR 8,3 4096 2 / bash 3599 boco txt REG 8,3 735004 3306367 /bin/bash bash 3599 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so bash 3599 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so bash 3599 boco mem REG 8,3 16428 492622 /lib/libdl-2.5.so bash 3599 boco mem REG 8,3 13276 492642 /lib/libtermcap.so.2.0.8 bash 3599 boco mem REG 8,3 46680 491080 /lib/libnss_files-2.5.so bash 3599 boco mem REG 8,3 25462 2588743 /usr/lib/gconv/gconv-modu les.cache bash 3599 boco mem REG 8,3 56452288 2492259 /usr/lib/locale/locale-ar chive bash 3599 boco 0u CHR 136,0 2 /dev/pts/0 bash 3599 boco 1u CHR 136,0 2 /dev/pts/0 bash 3599 boco 2u CHR 136,0 2 /dev/pts/0 bash 3599 boco 255u CHR 136,0 2 /dev/pts/0 java 3707 boco cwd DIR 8,3 4096 752939 /home/boco/script java 3707 boco rtd DIR 8,3 4096 2 / java 3707 boco txt REG 8,3 47308 3404707 /usr/local/jdk1.6.0_45/bi n/java java 3707 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so java 3707 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so java 3707 boco mem REG 8,3 208352 492627 /lib/libm-2.5.so java 3707 boco mem REG 8,3 16428 492622 /lib/libdl-2.5.so java 3707 boco mem REG 8,3 129716 492623 /lib/libpthread-2.5.so java 3707 boco mem REG 8,3 44060 492624 /lib/librt-2.5.so java 3707 boco mem REG 8,3 101404 492632 /lib/libnsl-2.5.so java 3707 boco mem REG 8,3 15032320 3178121 /usr/local/jdk1.6.0_45/jr e/lib/i386/client/classes.jsa java 3707 boco mem REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jr e/lib/rt.jar java 3707 boco mem REG 8,3 77119 3114849 /usr/local/jdk1.6.0_45/jr e/lib/i386/libzip.so java 3707 boco mem REG 8,3 46680 491080 /lib/libnss_files-2.5.so java 3707 boco mem REG 8,3 32768 327367 /tmp/hsperfdata_boco/3707 java 3707 boco mem REG 8,3 189222 3114847 /usr/local/jdk1.6.0_45/jr e/lib/i386/libjava.so java 3707 boco mem REG 8,3 6450682 3178118 /usr/local/jdk1.6.0_45/jr e/lib/i386/client/libjvm.so java 3707 boco mem REG 8,3 38142 3277888 /usr/local/jdk1.6.0_45/jr e/lib/i386/jli/libjli.so java 3707 boco mem REG 8,3 56701 3114846 /usr/local/jdk1.6.0_45/jr e/lib/i386/libverify.so java 3707 boco 0u CHR 136,0 2 /dev/pts/0 java 3707 boco 1u CHR 136,0 2 /dev/pts/0 java 3707 boco 2u CHR 136,0 2 /dev/pts/0 java 3707 boco 3r REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jr e/lib/rt.jar java 3707 boco 5w FIFO 0,6 15657 pipe java 3707 boco 6r FIFO 0,6 15658 pipe java 3707 boco 8r FIFO 0,6 15659 pipe sleep.sh 3717 boco cwd DIR 8,3 4096 752939 /home/boco/script sleep.sh 3717 boco rtd DIR 8,3 4096 2 / sleep.sh 3717 boco txt REG 8,3 735004 3306367 /bin/bash sleep.sh 3717 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so sleep.sh 3717 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so sleep.sh 3717 boco mem REG 8,3 16428 492622 /lib/libdl-2.5.so sleep.sh 3717 boco mem REG 8,3 13276 492642 /lib/libtermcap.so.2.0.8 sleep.sh 3717 boco 0r FIFO 0,6 15657 pipe sleep.sh 3717 boco 1w FIFO 0,6 15658 pipe sleep.sh 3717 boco 2w FIFO 0,6 15659 pipe sleep.sh 3717 boco 255r REG 8,3 47 752964 /home/boco/script/sleep.s h sleep 3718 boco cwd DIR 8,3 4096 752939 /home/boco/script sleep 3718 boco rtd DIR 8,3 4096 2 / sleep 3718 boco txt REG 8,3 19564 3306437 /bin/sleep sleep 3718 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so sleep 3718 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so sleep 3718 boco 0r FIFO 0,6 15657 pipe sleep 3718 boco 1w FIFO 0,6 15658 pipe sleep 3718 boco 2w FIFO 0,6 15659 pipe
3、 列出在某個埠執行的程式
[root@redhat5 ~]# lsof -i:23 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME xinetd 3309 root 5u IPv4 12942 TCP *:telnet (LISTEN) telnetd 3597 root 0u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED) telnetd 3597 root 1u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED) telnetd 3597 root 2u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED) [root@redhat5 ~]# lsof -i:22 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME sshd 3257 root 3u IPv6 12673 TCP *:ssh (LISTEN)
4、 列出埠在 22-23 之間的所有程式
[root@redhat5 ~]# lsof -i:22-23 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME sshd 3257 root 3u IPv6 12673 TCP *:ssh (LISTEN) xinetd 3309 root 5u IPv4 12942 TCP *:telnet (LISTEN) telnetd 3597 root 0u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED) telnetd 3597 root 1u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED) telnetd 3597 root 2u IPv4 14212 TCP localhost:telnet->localhost:49887 (ESTABLISHED)
5、只列出使用 IPv4 的開啟檔案
[root@redhat5 ~]# lsof -i 4 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME dhclient 2747 root 4u IPv4 9709 UDP *:bootpc portmap 2876 rpc 3u IPv4 10060 UDP *:sunrpc portmap 2876 rpc 4u IPv4 10061 TCP *:sunrpc (LISTEN) rpc.statd 2916 root 3u IPv4 10196 UDP *:975 rpc.statd 2916 root 6u IPv4 10183 UDP *:972 rpc.statd 2916 root 7u IPv4 10209 TCP *:978 (LISTEN) hpiod 3220 root 0u IPv4 12562 TCP redhat5.4-baby:2208 (LISTEN) python 3225 root 4u IPv4 12582 TCP redhat5.4-baby:2207 (LISTEN) cupsd 3272 root 4u IPv4 12754 TCP redhat5.4-baby:ipp (LISTEN) cupsd 3272 root 6u IPv4 12757 UDP *:ipp xinetd 3309 root 5u IPv4 12942 TCP *:telnet (LISTEN) sendmail 3333 root 4u IPv4 12940 TCP redhat5.4-baby:smtp (LISTEN) avahi-dae 3467 avahi 13u IPv4 13585 UDP *:mdns avahi-dae 3467 avahi 15u IPv4 13587 UDP *:36306 telnetd 3597 root 0u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED) telnetd 3597 root 1u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED) telnetd 3597 root 2u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED)
6、只列出使用 IPv6 的開啟檔案
[root@redhat5 ~]# lsof -i 6 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME sshd 3257 root 3u IPv6 12673 TCP *:ssh (LISTEN) avahi-dae 3467 avahi 14u IPv6 13586 UDP *:mdns avahi-dae 3467 avahi 16u IPv6 13588 UDP *:38402
7、根據程式pid列出開啟的檔案
[root@redhat5 ~]# lsof -p 3707 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME java 3707 boco cwd DIR 8,3 4096 752939 /home/boco/script java 3707 boco rtd DIR 8,3 4096 2 / java 3707 boco txt REG 8,3 47308 3404707 /usr/local/jdk1.6.0_45/bin /java java 3707 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so java 3707 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so java 3707 boco mem REG 8,3 208352 492627 /lib/libm-2.5.so java 3707 boco mem REG 8,3 16428 492622 /lib/libdl-2.5.so java 3707 boco mem REG 8,3 129716 492623 /lib/libpthread-2.5.so java 3707 boco mem REG 8,3 44060 492624 /lib/librt-2.5.so java 3707 boco mem REG 8,3 101404 492632 /lib/libnsl-2.5.so java 3707 boco mem REG 8,3 15032320 3178121 /usr/local/jdk1.6.0_45/jre /lib/i386/client/classes.jsa java 3707 boco mem REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jre /lib/rt.jar java 3707 boco mem REG 8,3 77119 3114849 /usr/local/jdk1.6.0_45/jre /lib/i386/libzip.so java 3707 boco mem REG 8,3 46680 491080 /lib/libnss_files-2.5.so java 3707 boco mem REG 8,3 32768 327367 /tmp/hsperfdata_boco/3707 java 3707 boco mem REG 8,3 189222 3114847 /usr/local/jdk1.6.0_45/jre /lib/i386/libjava.so java 3707 boco mem REG 8,3 6450682 3178118 /usr/local/jdk1.6.0_45/jre /lib/i386/client/libjvm.so java 3707 boco mem REG 8,3 38142 3277888 /usr/local/jdk1.6.0_45/jre /lib/i386/jli/libjli.so java 3707 boco mem REG 8,3 56701 3114846 /usr/local/jdk1.6.0_45/jre /lib/i386/libverify.so java 3707 boco 0u CHR 136,0 2 /dev/pts/0 java 3707 boco 1u CHR 136,0 2 /dev/pts/0 java 3707 boco 2u CHR 136,0 2 /dev/pts/0 java 3707 boco 3r REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jre /lib/rt.jar java 3707 boco 5w FIFO 0,6 15657 pipe java 3707 boco 6r FIFO 0,6 15658 pipe java 3707 boco 8r FIFO 0,6 15659 pipe
8、根據程式名稱列出開啟的檔案
[root@redhat5 ~]# lsof -c java COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME java 3707 boco cwd DIR 8,3 4096 752939 /home/boco/script java 3707 boco rtd DIR 8,3 4096 2 / java 3707 boco txt REG 8,3 47308 3404707 /usr/local/jdk1.6.0_45/bin /java java 3707 boco mem REG 8,3 125736 492620 /lib/ld-2.5.so java 3707 boco mem REG 8,3 1611564 492621 /lib/libc-2.5.so java 3707 boco mem REG 8,3 208352 492627 /lib/libm-2.5.so java 3707 boco mem REG 8,3 16428 492622 /lib/libdl-2.5.so java 3707 boco mem REG 8,3 129716 492623 /lib/libpthread-2.5.so java 3707 boco mem REG 8,3 44060 492624 /lib/librt-2.5.so java 3707 boco mem REG 8,3 101404 492632 /lib/libnsl-2.5.so java 3707 boco mem REG 8,3 15032320 3178121 /usr/local/jdk1.6.0_45/jre /lib/i386/client/classes.jsa java 3707 boco mem REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jre /lib/rt.jar java 3707 boco mem REG 8,3 77119 3114849 /usr/local/jdk1.6.0_45/jre /lib/i386/libzip.so java 3707 boco mem REG 8,3 46680 491080 /lib/libnss_files-2.5.so java 3707 boco mem REG 8,3 32768 327367 /tmp/hsperfdata_boco/3707 java 3707 boco mem REG 8,3 189222 3114847 /usr/local/jdk1.6.0_45/jre /lib/i386/libjava.so java 3707 boco mem REG 8,3 6450682 3178118 /usr/local/jdk1.6.0_45/jre /lib/i386/client/libjvm.so java 3707 boco mem REG 8,3 38142 3277888 /usr/local/jdk1.6.0_45/jre /lib/i386/jli/libjli.so java 3707 boco mem REG 8,3 56701 3114846 /usr/local/jdk1.6.0_45/jre /lib/i386/libverify.so java 3707 boco 0u CHR 136,0 2 /dev/pts/0 java 3707 boco 1u CHR 136,0 2 /dev/pts/0 java 3707 boco 2u CHR 136,0 2 /dev/pts/0 java 3707 boco 3r REG 8,3 52557932 2983708 /usr/local/jdk1.6.0_45/jre /lib/rt.jar java 3707 boco 5w FIFO 0,6 15657 pipe java 3707 boco 6r FIFO 0,6 15658 pipe java 3707 boco 8r FIFO 0,6 15659 pipe [root@redhat5 ~]# [root@redhat5 ~]# lsof | grep TestSleep [root@redhat5 ~]# jps 3707 TestSleep 3798 Jps
9、只顯示pid
[root@redhat5 ~]# lsof -t -u boco 3599 3707 3717 3718
10、列出所有偵聽和已建立的網路連線
[root@redhat5 ~]# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME dhclient 2747 root 4u IPv4 9709 UDP *:bootpc portmap 2876 rpc 3u IPv4 10060 UDP *:sunrpc portmap 2876 rpc 4u IPv4 10061 TCP *:sunrpc (LISTEN) rpc.statd 2916 root 3u IPv4 10196 UDP *:975 rpc.statd 2916 root 6u IPv4 10183 UDP *:972 rpc.statd 2916 root 7u IPv4 10209 TCP *:978 (LISTEN) hpiod 3220 root 0u IPv4 12562 TCP redhat5.4-baby:2208 (LISTEN) python 3225 root 4u IPv4 12582 TCP redhat5.4-baby:2207 (LISTEN) sshd 3257 root 3u IPv6 12673 TCP *:ssh (LISTEN) cupsd 3272 root 4u IPv4 12754 TCP redhat5.4-baby:ipp (LISTEN) cupsd 3272 root 6u IPv4 12757 UDP *:ipp xinetd 3309 root 5u IPv4 12942 TCP *:telnet (LISTEN) sendmail 3333 root 4u IPv4 12940 TCP redhat5.4-baby:smtp (LISTEN) avahi-dae 3467 avahi 13u IPv4 13585 UDP *:mdns avahi-dae 3467 avahi 14u IPv6 13586 UDP *:mdns avahi-dae 3467 avahi 15u IPv4 13587 UDP *:36306 avahi-dae 3467 avahi 16u IPv6 13588 UDP *:38402 telnetd 3597 root 0u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED) telnetd 3597 root 1u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED) telnetd 3597 root 2u IPv4 14212 TCP localhost:telnet->localhost:49 887 (ESTABLISHED)
相關文章
- 使用 lsof 查詢開啟的檔案(轉)
- 使用 lsof 查詢開啟的檔案 (ibm)IBM
- 新專案,新的起點,每天進步一點點
- Linux下用 lsof 命令查詢指定埠被哪個程式佔用Linux
- 相親原始碼前端開發知識點,每天進步一點點原始碼前端
- [每天進步一點點]mysql筆記整理(三):索引MySql筆記索引
- 日常Java練習題(每天進步一點點系列)Java
- [每天進步一點點]mysql筆記整理(二):事務與鎖MySql筆記
- 每天一個 Linux 命令(51): lsof 命令Linux
- linux命令 — lsof 檢視程式開啟那些檔案 或者 檢視檔案給那個程式使用Linux
- 每天進步一點,這樣的IT所謂笨鳥先飛
- 檔案查詢命令
- Unix 檔案查詢命令
- Excel_不開啟檔案進行跨工作簿查詢Excel
- linux下的find檔案查詢命令與grep檔案內容查詢命令Linux
- 改變ogg抽取程式檢查點檔案中的檢查點
- 一句話查詢出指定節點的葉子記錄
- win10 查詢指定型別檔案方法 win10怎麼查詢指定型別檔案Win10型別
- linux命令之----find用於指定目錄下查詢檔案Linux
- Python查詢包含指定字串的所有檔案Python字串
- linux 命令與檔案的查詢Linux
- linux開放指定埠命令Linux
- 一點點進步的OceanBase資料庫文件!資料庫
- LINUX 下查詢指定時間的所有檔案Linux
- linux 查詢檔案命令 findLinux
- 檔案內容查詢命令(轉)
- 每天學點node系列-fs檔案系統
- linux下檢視埠命令lsofLinux
- Web開發知識點彙總(每天積累一點點)Web
- centos 開啟防火牆指定埠CentOS防火牆
- 程式設計師的進步從每一天少寫一點程式碼開始程式設計師
- iWant:一個去中心化的點對點共享檔案的命令列工具中心化命令列
- [Mysql 查詢語句]——對查詢結果進一步的操作MySql
- 每天學點node系列-fs檔案系統(續)
- python檔案查詢之find命令Python
- Linux檔案內容查詢命令Linux
- 每天學一點JAVAJava
- 通過API開啟PDF檔案,並進行指定文字的搜尋API