lsof
1.列出所有打開文件
# lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 253,0 4096 2 /
init 1 root rtd DIR 253,0 4096 2 /
init 1 root txt REG 253,0 145180 147164 /sbin/init
init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so
init 1 root 0u CHR 1,3 0t0 3764 /dev/null
init 1 root 1u CHR 1,3 0t0 3764 /dev/null
init 1 root 2u CHR 1,3 0t0 3764 /dev/null
init 1 root 3r FIFO 0,8 0t0 8449 pipe
init 1 root 4w FIFO 0,8 0t0 8449 pipe
init 1 root 5r DIR 0,10 0 1 inotify
init 1 root 6r DIR 0,10 0 1 inotify
init 1 root 7u unix 0xc1513880 0t0 8450 socket
-
FD 文件類描述符
cwd current working directory
rtd root directory
txt program text (code and data)
mem memory-mapped file TYPE 文件類型
DIR – Directory
REG – Regular file
CHR – Character special file.
FIFO – First In First Out
2.列出用戶使用文件
# lsof -u tecmint
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1838 tecmint cwd DIR 253,0 4096 2 /
sshd 1838 tecmint rtd DIR 253,0 4096 2 /
sshd 1838 tecmint txt REG 253,0 532336 188129 /usr/sbin/sshd
sshd 1838 tecmint mem REG 253,0 19784 190237 /lib/libdl-2.12.so
sshd 1838 tecmint mem REG 253,0 122436 190247 /lib/libselinux.so.1
sshd 1838 tecmint mem REG 253,0 255968 190256 /lib/libgssapi_krb5.so.2.2
sshd 1838 tecmint mem REG 253,0 874580 190255 /lib/libkrb5.so.3.3
3.占用指定端口的pid
- 占用22號端口進(jìn)程
# lsof -i TCP:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN)
sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
- 占用用1-1024進(jìn)程端口進(jìn)程
# lsof -i TCP:1-1024
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN)
sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN)
sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)
sshd 1838 tecmint 3u IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)
sshd 1871 root 3r IPv4 15842 0t0 TCP 192.168.0.2:ssh->192.168.0.1:groove (ESTABLISHED)
httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN)
httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
4.列出所有網(wǎng)絡(luò)連接LISTENING & ESTABLISHED
# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc
rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954
rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN)
avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns
avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600
rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)
cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN)
cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN)
sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN)
master 1551 root 12u IPv4 12896 0t0 TCP localhost:smtp (LISTEN)
master 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN)
sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)
httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN)
httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
clock-app 2362 narad 21u IPv4 22591 0t0 TCP 192.168.0.2:45284->www.gov.com:http (CLOSE_WAIT)
chrome 2377 narad 61u IPv4 25862 0t0 TCP 192.168.0.2:33358->maa03s04-in-f3.1e100.net:http (ESTABLISHED)
chrome 2377 narad 80u IPv4 25866 0t0 TCP 192.168.0.2:36405->bom03s01-in-f15.1e100.net:http (ESTABLISHED)
5.通過pid查看進(jìn)程占用文件
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 253,0 4096 2 /
init 1 root rtd DIR 253,0 4096 2 /
init 1 root txt REG 253,0 145180 147164 /sbin/init
init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so
init 1 root mem REG 253,0 142472 189970 /lib/ld-2.12.so
6.kill指定用戶所有進(jìn)程
# kill -9 `lsof -t -u tecmint`
7. 查看文件(目錄)被哪個進(jìn)程占用
- 文件占用
lsof -t /lib/i386-linux-gnu/libaudit.so.1.0.0
- 目錄占用
lsof +D /usr/lib/locale
8.查找parent id
lsof -p [PID] -R
pidstat
安裝
yum -y install sysstat
參數(shù)
-C: 指定程序名稱
-u:默認(rèn)的參數(shù)族铆,顯示各個進(jìn)程的cpu使用統(tǒng)計
-r:顯示各個進(jìn)程的內(nèi)存使用統(tǒng)計
-d:顯示各個進(jìn)程的IO使用情況
-p:指定進(jìn)程號
-w:顯示每個進(jìn)程的上下文切換情況
-t:顯示選擇任務(wù)的線程的統(tǒng)計信息外的額外信息
-T { TASK | CHILD | ALL }
這個選項指定了pidstat監(jiān)控的理疙。TASK表示報告獨立的task,CHILD關(guān)鍵字表示報告進(jìn)程下所有線程統(tǒng)計信息材蹬。ALL表示報告獨立的task和task下面的所有線程迎吵。
注意:task和子線程的全局的統(tǒng)計信息和pidstat選項無關(guān)。這些統(tǒng)計信息不會對應(yīng)到當(dāng)前的統(tǒng)計間隔,這些統(tǒng)計信息只有在子線程kill或者完成的時候才會被收集鹤树。
-V:版本號
-h:在一行上顯示了所有活動,這樣其他程序可以容易解析逊朽。
-I:在SMP環(huán)境罕伯,表示任務(wù)的CPU使用率/內(nèi)核數(shù)量
使用
- To display only tasks whose command name includes the string comm
# pidstat -l -C pg_rman
Linux 3.10.0-693.21.1.el7.x86_64 (NBU-RESTOR-PG-CWT01) 03/05/2019 _x86_64_ (2 CPU)
06:24:55 PM UID PID %usr %system %guest %CPU CPU Command
06:24:55 PM 501 13911 0.07 0.02 0.00 0.09 0 pg_rman restore --recovery-target-time=2019-02-27 18:06:06 --debug
- To report I/O statistics
# pidstat -d 1
Linux 3.10.0-693.21.1.el7.x86_64 (NBU-RESTOR-PG-CWT01) 03/05/2019 _x86_64_ (2 CPU)
06:27:52 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:53 PM 501 13911 36141.18 54447.06 0.00 pg_rman
06:27:53 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:54 PM 501 13911 53248.00 70344.00 0.00 pg_rman
06:27:54 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:55 PM 501 13911 6124.00 28240.00 0.00 pg_rman
06:27:55 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:56 PM 501 13911 28160.00 147880.00 0.00 pg_rman
06:27:56 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:57 PM 501 13911 33272.00 162480.00 0.00 pg_rman
06:27:57 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:58 PM 0 751 0.00 12.00 0.00 rsyslogd
06:27:58 PM 501 13911 32768.00 122440.00 0.00 pg_rman
06:27:58 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:27:59 PM 501 13911 32768.00 112304.00 0.00 pg_rman
06:27:59 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:28:00 PM 501 13911 32768.00 112216.00 0.00 pg_rman
06:28:00 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
06:28:01 PM 501 13911 36864.00 136288.00 0.00 pg_rman
- To display the process command name and all its arguments
[root@NBU-RESTOR-PG-CWT01 ~]# pidstat -l
Linux 3.10.0-693.21.1.el7.x86_64 (NBU-RESTOR-PG-CWT01) 03/05/2019 _x86_64_ (2 CPU)
06:28:50 PM UID PID %usr %system %guest %CPU CPU Command
06:28:50 PM 0 1 0.00 0.02 0.00 0.02 0 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
06:28:50 PM 0 2 0.00 0.00 0.00 0.00 1 kthreadd
06:28:50 PM 0 3 0.00 0.00 0.00 0.00 0 ksoftirqd/0
06:28:50 PM 0 7 0.00 0.00 0.00 0.00 0 migration/0
06:28:50 PM 0 9 0.00 0.01 0.00 0.01 1 rcu_sched
06:28:50 PM 0 10 0.00 0.00 0.00 0.00 0 watchdog/0
06:28:50 PM 0 11 0.00 0.00 0.00 0.00 1 watchdog/1
06:28:50 PM 0 12 0.00 0.00 0.00 0.00 1 migration/1
06:28:50 PM 0 13 0.00 0.00 0.00 0.00 1 ksoftirqd/1
06:28:50 PM 0 19 0.00 0.00 0.00 0.00 1 khungtaskd
06:28:50 PM 0 30 0.00 0.06 0.00 0.06 0 kswapd0
06:28:50 PM 0 32 0.00 0.00 0.00 0.00 0 khugepaged
06:28:50 PM 0 99 0.00 0.00 0.00 0.00 1 kauditd
06:28:50 PM 0 282 0.00 0.00 0.00 0.00 1 scsi_eh_0
06:28:50 PM 0 456 0.00 0.03 0.00 0.03 1 xfsaild/dm-0
06:28:50 PM 0 525 0.00 0.01 0.00 0.01 1 /usr/lib/systemd/systemd-journald
06:28:50 PM 0 549 0.00 0.00 0.00 0.00 1 /usr/lib/systemd/systemd-udevd
06:28:50 PM 0 555 0.00 0.00 0.00 0.00 1 /usr/sbin/lvmetad -f
06:28:50 PM 0 684 0.00 0.00 0.00 0.00 1 xfsaild/dm-2
06:28:50 PM 0 699 0.00 0.00 0.00 0.00 0 xfsaild/dm-3
06:28:50 PM 0 719 0.00 0.00 0.00 0.00 1 /sbin/auditd
06:28:50 PM 81 742 0.00 0.00 0.00 0.00 0 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
06:28:50 PM 0 744 0.00 0.00 0.00 0.00 0 /usr/lib/systemd/systemd-logind
06:28:50 PM 999 749 0.00 0.00 0.00 0.00 1 /usr/lib/polkit-1/polkitd --no-debug
06:28:50 PM 0 750 0.00 0.01 0.00 0.01 1 /usr/sbin/irqbalance --foreground
06:28:50 PM 0 751 0.01 0.00 0.00 0.01 0 /usr/sbin/rsyslogd -n
06:28:50 PM 998 754 0.00 0.00 0.00 0.00 0 /usr/sbin/chronyd
06:28:50 PM 0 757 0.00 0.00 0.00 0.00 1 /usr/sbin/NetworkManager --no-daemon
06:28:50 PM 0 765 0.05 0.04 0.00 0.09 0 /usr/bin/vmtoolsd
06:28:50 PM 0 770 0.00 0.00 0.00 0.00 0 /usr/sbin/crond -n
06:28:50 PM 0 783 0.00 0.00 0.00 0.00 0 login -- root
06:28:50 PM 0 833 0.00 0.00 0.00 0.00 0 kworker/0:1H
06:28:50 PM 0 929 0.00 0.00 0.00 0.00 1 kworker/1:1H
06:28:50 PM 0 1127 0.01 0.00 0.00 0.01 1 /usr/bin/python -Es
- 指定pid
pidstat -p 1234