+++++++進程管理工具 top/htop/glances/dstat命令
pstree,ps,pgrep,pkill,pidof
top,htop
glance,pmap,
vmstat,dstat
kill
job,bg,fg,nohup
sar,tsar,iosar,iftop
註釋:
[string] 後臺的內核線程
1、線程內共享進程內存,共享打開文件描述符(文件內容)
2、線程是進程的子單位
3、調度、跟蹤困難
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
USER: 進程用戶
PID: 進程PID
%CPU: 運行占據CPU時間百分比
VSZ:虛擬內存集:可交換出的數據
RSS: 常駐內存集:不可交換出的數據
TTY: 通過哪個終端啟動
STAT:進程狀態
R(Running),S(Interrupt Sleeping),D(uninterrupt Sleeping),T(Stopped),z(zombie)
+,l + 前臺,l後臺
N,< N:低優先級
s 領導進程,
START: 進程啟動時間
TIME: 進程運行過程占據CPU累積時長
COMMAND: 由什麽命令啟動的相關進程
PR NI VIRT RES SHR S
PR 優先級
NI Nice值
VIRT 相當於VRT
RES 相當於RSS
SHR shared
S STATE
PPID PRI PSR RTPRIO
ppid: 父進程id,0代表 天造地設
PRI: priority
PSR: 運行在哪個cpu上
rtprio: realtime priority 實時優先級
C cpu編號
top - 17:45:26 up 3:12, 1 user, load average: 0.00, 0.01, 0.05
%Cpu(s): 0.4 us, 0.2 sy, 0.0 ni, 99.2 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
top - 當前系統時間 up 運行時長, 用戶數, 平均負載:過去1分鐘,5分鐘,15分鐘
平均負載:CPU隊列中等待運行的進程數
%Cpu(s): 0.4 us, 0.2 sy, 0.0 ni, 99.2 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
所有CPU: user space, system space, nice , idle , wait , hardware interrupt, software interrupt, stole
us: 用戶空間占據cpu時間百分比
wa: 等待IO完成的時間。時間長建議換硬盤
st:被虛擬化技術偷走的時間
1、pstree顯示進程樹
CentOS 7
[[email protected] ~]# pstree systemd─┬─AliYunDun───13*[{AliYunDun}]
CentOS 6
init(upstart)
CentOS 5
init
2、ps
System V 風格
ps option
a 與終端相關的進程
x 與終端無關的進程
u 與用戶相關的進程
Z 安全標簽
o 自定義顯示格式
常用組合:
# ps aux
# ps axZ
# ps axo ppid,psr,ni,pri,rtprio,cmd
BSD風格
ps option
-e 相當於 ax
-f/-F 顯示詳細信息
-H hierarchy 層級結構顯示
使用示例
System V
[[email protected] ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND [root[email protected] ~]# ps axZ LABEL PID TTY STAT TIME COMMAND [[email protected] ~]# ps -ef UID PID PPID C STIME TTY TIME CMD [[email protected] ~]# ps -eF UID PID PPID C SZ RSS PSR STIME TTY TIME CMD [[email protected] ~]# ps -eFH | head -n 1 UID PID PPID C SZ RSS PSR STIME TTY TIME CMD [[email protected] ~]# ps -exo pid,ppid,ni,psr,pri,rtprio,time,cmd PID PPID NI PSR PRI RTPRIO TIME CMD
3、pgrep,pkill
pgrep [option] pattern
pkill [option] pattern
-u euid 有效用戶(suid的屬主)
-U uid 真實用戶
-t terminal 與指定終端相關的進程
-a 完整格式
-l 顯示進程名
-P PPID 顯示PPID的子進程列表
[[email protected] ~]# pgrep sshd 4472 10829 [[email protected] ~]# pgrep sshd -l 4472 sshd 10829 sshd [[email protected] ~]# pgrep sshd -a 4472 /usr/sbin/sshd -D 10829 sshd: [email protected]/0 [[email protected] ~]# pgrep -P 4472 10829 [[email protected] ~]# pgrep -t pts/0 10831 [[email protected] ~]# pgrep -t pts/0 -l 10831 bash [[email protected] ~]# pgrep -t pts/0 -a 10831 -bash
4、top命令
top option
-d 刷新時間間隔
-b 批次顯示
-n # 顯示多少批次 與-b連勝
[[email protected] ~]# top -d 0.0000000000000000000000000000000001 [[email protected] ~]# top -d 0.0000000000000000000000000000000001 -b [[email protected] ~]# top -d 0.0000000000000000000000000000000001 -b -n 10
排序
P %CPU 占據CPU百分比(過去幾秒占據CPU百分比:幾秒由刷新定義)
M %MEM
T TIME
顯示
uptime: l
tasks及cpu: t
每個cpu: 1
Mem/Swap: m
修改
殺死: k
退出: q
修改刷新時間間隔: s
6、htop 交互式進程查看命令
Fedora-EPEL
獲取幫助
F1
選定進程
s syscall,顯示進程相關的信息
t tree ,層級結構顯示
l 顯示進程打開的文件及共享庫
a 設置進程和cpu親源性,進程只能運行在某個cpu上,增加緩存命中率
F6 > 以指定字段排序
[[email protected] ~]# dnf install htop
7、vmstat命令 虛擬內存工具virtual memory state
vmstat [delay[count]]
delay 延遲幾秒刷新一次
count 刷新幾次
-p partiton
內存信息查看
vmstat -s
/proc/memoinfo
vmstat註釋
[[email protected] ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 3 0 0 208496 37940 465952 0 0 21 8 107 256 0 0 99 0 0
procs 進程
r running
b block
memory 內存
swpd 交換內存使用的總量
free 空閑物理內存總量
buff 內存中buff大小
cache 內存中cache大小
swap 交換分區
si 從物理內存加載至swap的速率
so 從swap加載至物理內存的速率
io
bi input 從磁盤加載至內存的速率(kb/s)
bo output 從內存加載至磁盤的速率
system 系統
in/int interrupt 中斷,每秒多少個中斷
cs context switch 進程上下文切換次數(保存現場和恢復現場的切換次數)
cpu
us user space
sy system space/kernel space
id idle
wa wait 等待IO完成的時間比率
st stole 被虛擬化技術偷走的比率
8、pmap報告內存映射表
pmap [-x|-p|-d|-q] pid
-x extend顯示擴展格式
-p 顯示映射的完整路徑
-d 顯示設備信息
-q 不顯示首尾信息
查看內存映射表
/proc/PID/maps
pmap pid
線性內存地址和物理地址映射關系
進程只知道線性地址
1、進程將打開的文件存儲在進程內存地址空間中,分段存儲
2、進程劃分的空間為4K的整數倍(內存頁的大小為4K)
3、heap堆、stack棧、anon: anonymous 匿名頁,進程自己存儲數據的位置,不能交換出去
Address Kbytes RSS Dirty Mode Mapping 00007ff5a8000000 164 12 12 rw--- [ anon ] 00007ff5a8029000 65372 0 0 ----- [ anon ] 00007ff5ac94d000 4 0 0 ----- [ anon ] 00007ff5ac94e000 8192 8 8 rw--- [ anon ] 00007ff5ad14e000 4 0 0 ----- [ anon ] 00007ff5ad14f000 8192 8 8 rw--- [ anon ] 00007ff5ad94f000 16 8 0 r-x-- libuuid.so.1.3.0 00007ff5ad953000 2044 0 0 ----- libuuid.so.1.3.0 00007ff5adb52000 4 4 4 r---- libuuid.so.1.3.0 00007ff5adb53000 4 4 4 rw--- libuuid.so.1.3.0 00007ff5adb54000 228 40 0 r-x-- libblkid.so.1.1.0 00007ff5adb8d000 2048 0 0 ----- libblkid.so.1.1.0 00007ff5add8d000 12 12 12 r---- libblkid.so.1.1.0 00007ff5add90000 4 4 4 rw--- libblkid.so.1.1.0
使用示例
1、默認 [[email protected] ~]# pmap 1 | head 1: /usr/lib/systemd/systemd --switched-root --system --deserialize 21 00007ff5a8000000 164K rw--- [ anon ] 00007ff5a8029000 65372K ----- [ anon ] 00007ff5ac94d000 4K ----- [ anon ] 00007ff5ac94e000 8192K rw--- [ anon ] 00007ff5ad14e000 4K ----- [ anon ] 00007ff5ad14f000 8192K rw--- [ anon ] 00007ff5ad94f000 16K r-x-- libuuid.so.1.3.0 00007ff5ad953000 2044K ----- libuuid.so.1.3.0 00007ff5adb52000 4K r---- libuuid.so.1.3.0 2、顯示詳細 [[email protected] ~]# pmap -x 1 | head 1: /usr/lib/systemd/systemd --switched-root --system --deserialize 21 Address Kbytes RSS Dirty Mode Mapping 00007ff5a8000000 164 12 12 rw--- [ anon ] 00007ff5a8029000 65372 0 0 ----- [ anon ] 00007ff5ac94d000 4 0 0 ----- [ anon ] 00007ff5ac94e000 8192 8 8 rw--- [ anon ] 00007ff5ad14e000 4 0 0 ----- [ anon ] 00007ff5ad14f000 8192 8 8 rw--- [ anon ] 00007ff5ad94f000 16 8 0 r-x-- libuuid.so.1.3.0 00007ff5ad953000 2044 0 0 ----- libuuid.so.1.3.0 3、靜默 [[email protected] ~]# pmap -xq 1 | head 1: /usr/lib/systemd/systemd --switched-root --system --deserialize 21 00007ff5a8000000 164 12 12 rw--- [ anon ] 00007ff5a8029000 65372 0 0 ----- [ anon ] 00007ff5ac94d000 4 0 0 ----- [ anon ] 00007ff5ac94e000 8192 8 8 rw--- [ anon ] 00007ff5ad14e000 4 0 0 ----- [ anon ] 00007ff5ad14f000 8192 8 8 rw--- [ anon ] 00007ff5ad94f000 16 8 0 r-x-- libuuid.so.1.3.0 00007ff5ad953000 2044 0 0 ----- libuuid.so.1.3.0 00007ff5adb52000 4 4 4 r---- libuuid.so.1.3.0 4、顯示完整map [[email protected] ~]# pmap -xqp 1 | head 1: /usr/lib/systemd/systemd --switched-root --system --deserialize 21 00007ff5a8000000 164 12 12 rw--- [ anon ] 00007ff5a8029000 65372 0 0 ----- [ anon ] 00007ff5ac94d000 4 0 0 ----- [ anon ] 00007ff5ac94e000 8192 8 8 rw--- [ anon ] 00007ff5ad14e000 4 0 0 ----- [ anon ] 00007ff5ad14f000 8192 8 8 rw--- [ anon ] 00007ff5ad94f000 16 8 0 r-x-- /usr/lib64/libuuid.so.1.3.0 00007ff5ad953000 2044 0 0 ----- /usr/lib64/libuuid.so.1.3.0 00007ff5adb52000 4 4 4 r---- /usr/lib64/libuuid.so.1.3.0
9、glances命令
galances [options...]
-s -B IPADDR 服務端模式
-c IPADDR 客戶端模式
-b 以字節顯示網絡速率
-m/-n/-d 控制mount,network,disk模塊的顯示
-t # 延遲間隔
-1 每個cpu信息單獨顯示
-f /path/to/somefile galance顯示的結果保存至文件中
-o {HTML|CSV} 文件的格式
galance內建命令
a Sort processes automatically l Show/hide logs c Sort processes by CPU% b Bytes or bits for network I/O m Sort processes by MEM% w Delete warning logs p Sort processes by name x Delete warning and critical logs i Sort processes by I/O rate 1 Global CPU or per-CPU stats d Show/hide disk I/O stats h Show/hide this help screen f Show/hide file system stats t View network I/O as combination n Show/hide network stats u View cumulative network I/O s Show/hide sensors stats q Quit (Esc and Ctrl-C also work) y Show/hide hddtemp stats
服務端
[[email protected] ~]# glances -s -B 192.168.1.103 -p 8888 -P 1234 Glances server is running on 192.168.1.103:8888
客戶端
[[email protected] ~]# glances -c 192.168.1.103 -p 8888 Error: Connection to server failed. Bad password. [[email protected] ~]# glances -c 192.168.1.103 -p 8888 -P 1234 [[email protected] ~]# Connected to 192.168.1.103 | Press ‘h‘ for help
10、dstat命令
vmstat替換工具
dstat options
-c/-C #,#,..,total
-d/-D total,/dev/sda,/dev/sdb,...
-g page顯示內存數據交換的速率
-i 顯示中斷速率
-l --load 顯示平均負載
-m,--mem 顯示內存信息
-n,--net 顯示網絡信息
-p,--proc 進程狀態
-r,--io 顯示io相關的統計數據
-s,--swap 顯示swap 相關的統計數據
-t,--time 時間輸出
--aio 異步io
--fs,--filesystem 顯示文件系統
--ipc (message queue(signal),semephores,shared memory)
--lock 文件鎖
--socket (total,udp,tcp,raw,ip-fragment分片)
--tcp (listen,established,syn,time_wait,close)
--udp (listen,active)
--unix (datagram, stream, listen, active)
PLUGIN:
-a,相當於 -cdngy
--battery 電池
--battery-remain 電池剩余的時間(ACPI支持)
--disk-tps 磁盤每秒鐘的事務量(need ACPI)
--mysql5-conn 對mysql運行命令的統計
--top-cpu 顯示最消耗CPU的進程
--top-int 顯示中斷數據最大的進程
--top-io 最消耗IO的進程
--top-mem 最消耗內存的進程
--top-latency 顯示延遲最大的進程
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| read writ| recv send| in out | int csw total-cpu-usage cpu使用狀態 dsk/total 磁盤讀寫速率 net/total 網絡的收發速率 paging 頁面的換進換出(內存頁面,交換內存。 si,so) system 中斷和上下文切換 (int: interrupt, csw: context swtich。 in,cs )
使用示例
1、cpu相關的信息
[[email protected] ~]# dstat -c ----total-cpu-usage---- usr sys idl wai hiq siq [[email protected] ~]# dstat -C total,0 Terminal width too small, trimming output. ----total-cpu-usage------------cpu0-usage------ -dsk/total- -net/total- ---paging--> usr sys idl wai hiq siq:usr sys idl wai hiq siq| read writ| recv send| in out >
2、硬盤相關
[[email protected] ~]# dstat -d -dsk/total- read writ [[email protected] ~]# dstat -D total,/dev/sda ----total-cpu-usage---- -dsk/total----dsk/sda-- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| read writ: read writ| recv send| in out | int csw
3、頁面
[[email protected] ~]# dstat -g ---paging--
4、顯示中斷
[[email protected] ~]# dstat -i ----interrupts---
5、平均負載
[[email protected] ~]# dstat -l ---load-avg--- 1m 5m 15m
6、內存
[[email protected] ~]# dstat -m ------memory-usage----- used buff cach free
7、網絡
[[email protected] ~]# dstat -n -net/total- recv send
8、進程
---procs--- run blk new run 運行進程的數據 new 創建新進程的數據
9、io
[[email protected] ~]# dstat -r --io/total- read writ
10、swap
[[email protected] ~]# dstat -s ----swap--- used free
11、時間
[[email protected] ~]# dstat -t ----system---- date/time
12、異步IO
[[email protected] ~]# dstat --aio async #aio
13、文件系統
[[email protected] ~]# dstat --fs --filesystem- files inodes
14、進程通信
[[email protected] ~]# dstat --ipc --sysv-ipc- msg sem shm
15、文件鎖
[[email protected] ~]# dstat --lock ---file-locks-- pos lck rea wri
16、套接字
[[email protected] ~]# dstat --socket ------sockets------ tot tcp udp raw frg
17、tcp連接
[[email protected] ~]# dstat --tcp ----tcp-sockets---- lis act syn tim clo
18、udp連接
[[email protected] ~]# dstat --udp --udp-- lis act
19、unix連接
[[email protected] ~]# dstat --unix --unix-sockets- dgm str lis act
20、-a
[[email protected] ~]# dstat -a 1 1 ----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| read writ| recv send| in out | int csw
[[email protected] ~]# dstat --disk-util sda- util
22、文件系統空閑
[[email protected] ~]# dstat --freespace -----/----------/shm-------/boot-------/home--------/usr--------/var--- used free: used free: used free: used free: used free: used free 3101M 44.9G: 0 121M:57.4M 419M: 260M 4534M:2091M 12.3G: 714M 9001M
23、插件註釋
[[email protected] ~]# dstat --helloworld plugin-title counter Hello world! Hello world! Hello world! Hello world!
24、最消耗cpu進程
[[email protected] ~]# dstat --top-cpu -most-expensive- cpu process AliYunDun 0.2 aliyun-servic1.0
25、中斷次數最多
[[email protected] ~]# dstat --top-in ---most-frequent---- interrupt ata_piix 1 virtio0-input.0 1
26、io次數
----most-expensive---- i/o process systemd 29k 10k sshd: [email protected] 146B 196B
27、內存消耗最大
[[email protected] ~]# dstat --top-mem --most-expensive- memory process mysqld 123M
28、延遲最大的進程
[[email protected] ~]# dstat --top-latency --highest-total-- latency process rcu_sched 882
11、kill命令
查看信號:
kill -l
man 7 signal
SIGHUP: 不關閉進程重讀配置文件。服務程序
SIGINT: 終止信號(interrupt) 相當於 Ctrl + c
SIGKILL: 殺死正在運行的進程 ,直接終止,
SIGTERM: 終止指定的進程(優雅) ,處理的數據存回磁盤中,再終止
SIGCONT 讓停止態的信號,continue
SIGSTOP 讓運行中的進程,stop
SIGNAL:
1、全名 SIGHUP
2、簡名 HUP
3、數字標識 1
發信號
kill -SIGNAL pid
killall -SIGNAL 進程名
12、作業
jobs一個任務可能包含單個進程也可能包含多個進程
前臺作業:與終端相關,占據命令提示符
後臺作業:與終端相關,不占據命令提示符
送到後臺剝離與終端關系: # nohup COMMMAND &
作業送到後臺:
運行中的程序:Ctrl + z
尚未啟動的程序:COMMAND &
查看作業: # jobs
+ 沒有作業號,默認管理的作業
- 下一個
[#]作業號
作業管理
送到前臺: fg [[%]JOB_NUM]
送到後臺: bg [[%]JOB_NUM]
終止作業: kill -SIGNAL %JOB)NUM
13、nice值
動態優先級:進程消耗過多的cpu資源時,內核自動調整,
靜態優先級: 100-139 (數據越小,優先級越高)
實時優先級: 0-99 (數字越大,優先級越高)
進程默認啟動優先級120,nice值為 0
nice [-n # ] command
-n # 啟動後以#值作為Nice值運行,不給-n #,默認nice=10
renice -n # command
-n # 重新設置優先級
查看nice值: # ps axo pid,command,ni
14、sar,tsar,iosar,iftop
sar
-u cpu
-d disk
-B 交換頁數據
-b 報告I\O傳輸速率
詳解:-B
pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s
pgpgin/s 物理內存到磁盤swap
fault 缺頁異常發生的速率
majflt/s major faults
pgfree/s page free place on
pgscank/s kswapd daemon scan pages
pgscnd/s page scan directly
pgsteal/s page reclaimed from cache(pagecache and swap-cache)
詳解: -u 報告cpu使用率
CPU %user %nice %system %iowait %steal %idle
詳解: -b
tps rtps wtps bread/s bwrtn/s
t 總傳輸
rtps 讀傳輸
wtps 寫傳輸
bread/s 從塊讀
bwrtn/s 寫到塊
tsar
iostat
Report Central Processing Unit (CPU) statistics and input/output statistics
for devices and partitions.
Central Processing Unit (CPU) 中央處理單元cpu
statistics 統計數據
for prep.為,為了;傾向於;關於;當作
為設備和分區報告CPU,I/O統計數據
iostat [-u | -d | -h | -k | -m]
-u cpu
-d disk
-h human readable
-k KB
-m MB
-N LVM2統計數據
CPU Utilization Report(Utilization 利用率)
%user 運行用戶級別/用戶空間的進程CPU利用率百分比
%nice nice priority占據CPU利用百分比
%system system level(kernel)
%iowait 系統有突出IO時,CPU空閑百分比。進程等待IO的百分比
%steal 被虛擬化技術偷走的時間
%idle CPU空閑和系統無IO所占時間百分比
Device Utilization Report
Device 顯示dev目錄下的設備或Partition名
tps 顯示每秒IO請求傳輸至設備的數量,IO請求可以是單個,或多個IO請求組合成單個,大小不確定
kB_read/s 每秒從塊或扇區讀取數據量
kB_wrtn/s 每秒寫
kB_read 總讀
kB_wrtn 總寫
rrqm/s queued to device read request merged 合並讀請求隊列的速率
wrqm/s 合並寫請求速率
iftop
1)獲取命令的信息 命令行模式顯示網絡接口帶寬統計數據“為什麽我的ADSL連接如此慢”
[[email protected] ~]# dnf info iftop Using metadata from Wed Aug 23 00:36:04 2017 可安裝的軟件包 名稱 : iftop 架構 : x86_64 時期 : 0 版本 : 1.0 發布 : 0.14.pre4.el7 大小 : 52 k 倉庫 : epel 概要 : Command line tool that displays bandwidth usage on an interface URL : http://www.ex-parrot.com/~pdw/iftop/ 協議 : GPLv2+ 描述 : iftop does for network usage what top(1) does for CPU usage. It listens to : network traffic on a named interface and displays a table of current bandwidth : usage by pairs of hosts. Handy for answering the question "why is our ADSL link : so slow?".
2)安裝包
[[email protected] ~]# dnf install iftop
3)獲取幫助
# man iftop 顯示主機接口之上的網絡帶寬使用情況
iftop -h | [-nNpblBP] [-i interface] [-f filter code] [-F net/mask] [-G net6/mask6]
默認情況會反解主機名,-n選項或運行中使用r命令關閉dns反解
默認會顯示通過過濾器的所有IP包,IP包的方向是通過接口的方向
-F NETWORK 顯示給定網絡輸入或輸出的數據包
忽略廣播數據包: not ether host ff:ff:ff:ff:ff:ff
過濾僅顯示web數據包,排除:port http and not host webcache.example.com
用戶浪費了多少帶寬弄清楚網絡如此慢:icmp
-h 獲取幫助
-n 不從IP包中反解主機名
-N 不反解端口號
-p promise模式,流量不經過此接口也會統計
-P 打開端口顯示
-b bar開關
-m #[km] 僅顯示由#指定的寬帶比例上限對應的數據包
-i IFACE 指監聽接口
-f filter code 過濾
-F net/mask 過濾僅顯示什麽ip的數據包,包的方向由網絡邊界決定
-c config_file 給出一個臨時配置文件,默認 ~/.iftoprc
內建命令
S ,D , P ,p ,n ,N
Host display: General: n - toggle DNS host resolution P - pause display s - toggle show source host h - toggle this help display d - toggle show destination host b - toggle bar graph display t - (revice,sent,two line,one line) B - cycle bar graph average T - toggle cumulative line totals Port display: j/k - scroll display N - toggle service resolution f - edit filter code S - toggle show source port l - set screen filter D - toggle show destination port L - lin/log scales p - toggle port display ! - shell command q - quit Sorting: 1/2/3 - sort by 1st/2nd/3rd column < - sort by source name > - sort by dest name o - freeze current order
顯示註釋
每個字段10秒平均流量顯示 foo.example.com => bar.example.com 1Kb 500b 100b 流量從 foo.example.com 到 bar.example.com 1Kb 前2秒數據被接收或發送的速率 500b 前10秒 100b 前40秒 過濾後的總傳輸流量 最近40s高峰流量 過去2s,10s,40s部傳輸速率
本文出自 “Reading” 博客,請務必保留此出處http://sonlich.blog.51cto.com/12825953/1958550
+++++++進程管理工具 top/htop/glances/dstat命令