進程管理相關命令
pstree: 命令以樹狀圖的方式展現進程之間的派生關係,顯示效果比較直觀。
選項:
-a:顯示每個程序的完整指令,包含路徑,參數或是常駐服務的標示;
-c:不使用精簡標示法;
-G:使用VT100終端機的列繪圖字符;
-h:列出樹狀圖時,特別標明現在執行的程序;
-H<程序識別碼>:此參數的效果和指定"-h"參數類似,但特別標明指定的程序;
-l:採用長列格式顯示樹狀圖;
-n:用程序識別碼排序。預設是以程序名稱來排序;
-p:顯示程序識別碼;
-u:顯示用戶名稱;
-U:使用UTF-8列繪圖字符;
-V:顯示版本信息。
pstree 常用 選項:
顯示當前所有進程的進程號和進程id
[root@lvs data]# pstree -p
init(1)─┬─abrtd(1813)
├─acpid(1525)
├─atd(1855)
├─auditd(1412)───{auditd}(1413)
├─console-kit-dae(1891)─┬─{console-kit-da}(1892)
│ ├─{console-kit-da}(1893)
│ ├─{console-kit-da}(1894)
│ ├─{console-kit-da}(1895)
│ ├─{console-kit-da}(1896)
│ ├─{console-kit-da}(1897)
│ ├─{console-kit-da}(1898)
│ ├─{console-kit-da}(1899)
│ ├─{console-kit-da}(1900)
│ ├─{console-kit-da}(1901)
│ ├─{console-kit-da}(1902)
│ ├─{console-kit-da}(1903)
│ ├─{console-kit-da}(1904)
│ ├─{console-kit-da}(1905)
│ ├─{console-kit-da}(1906)
│ ├─{console-kit-da}(1907)
顯示所有進程的所有詳細信息,遇到相同的進程名可以壓縮顯示。
[root@lvs data]# pstree -a
init
├─abrtd
├─acpid
├─atd
├─auditd
│ └─{auditd}
├─console-kit-dae --no-daemon
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
│ ├─{console-kit-da}
ps: 命令報告當前系統的進程狀態
Linux系統各進程的相關信息均保存在/proc/PID目錄下的各文件中
ps 支持三種風格的格式
1、UNIX options, which may be grouped and must be preceded by a dash.
UNIX選項,可以分組,並且必須以 '-' 開頭。
如 -A -e
2、BSD options, which may be grouped and must not be used with a dash.
BSD選項,可以分組,不能和 '-' 一起使用。
如 a
3、GNU long options, which are preceded by two dashes.
GNU長選項,前面有兩個'--'。
如 --help
選項:
a 選項包括所有終端中的進程
x 選項包括不鏈接終端的進程
u 選項顯示進程所有者的信息
f 選項顯示進程樹,相當於 --forest
k|--sort 屬性 對屬性排序,屬性前加- 表示倒序
o 屬性… 選項顯示定製的信息 pid、cmd、%cpu、%mem
L 顯示支持的屬性列表
-C cmdlist 指定命令,多個命令用,分隔
-L 顯示線程
-e: 顯示所有進程,相當於-A
-f: 顯示完整格式程序信息
-F: 顯示更完整格式的進程信息
-H: 以進程層級格式顯示進程相關信息
-u userlist 指定有效的用戶ID或名稱
-U userlist 指定真正的用戶ID或名稱
-g gid或groupname 指定有效的gid或組名稱
-G gid或groupname 指定真正的gid或組名稱
-p pid 顯示指pid的進程
--ppid pid 顯示屬於pid的子進程
-M 顯示SELinux信息,相當於Z
ps輸出屬性
VSZ: Virtual memory SiZe,虛擬內存集,線性內存
RSS: ReSident Size, 物理內存使用數
ni: nice值
pri: priority 優先級
psr: processor CPU編號
rtprio: 實時優先級
STAT:進程狀態
R:running
S: interruptable sleeping
D: uninterruptable sleeping
T: stopped
Z: zombie
+: 前臺進程
l: 多線程進程
L:內存分頁並帶鎖
N:低優先級進程
<: 高優先級進程
s: session leader,會話(子進程)發起者
常用組合命令:
[root@lvs ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 19352 1640 ? Ss Aug27 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S Aug27 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Aug27 0:00 [migration/0]
root 4 0.0 0.0 0 0 ? S Aug27 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S Aug27 0:00 [stopper/0]
root 6 0.0 0.0 0 0 ? S Aug27 0:00 [watchdog/0]
root 7 0.0 0.0 0 0 ? S Aug27 0:00 [migration/1]
root 8 0.0 0.0 0 0 ? S Aug27 0:00 [stopper/1]
root 9 0.0 0.0 0 0 ? S Aug27 0:00 [ksoftirqd/1]
root 10 0.0 0.0 0 0 ? S Aug27 0:00 [watchdog/1]
root 11 0.0 0.0 0 0 ? S Aug27 0:07 [events/0]
root 12 0.1 0.0 0 0 ? S Aug27 2:42 [events/1]
root 13 0.0 0.0 0 0 ? S Aug27 0:00 [events/0]
root 14 0.0 0.0 0 0 ? S Aug27 0:00 [events/1]
[root@lvs ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug27 ? 00:00:01 /sbin/init
root 2 0 0 Aug27 ? 00:00:00 [kthreadd]
root 3 2 0 Aug27 ? 00:00:00 [migration/0]
root 4 2 0 Aug27 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 Aug27 ? 00:00:00 [stopper/0]
root 6 2 0 Aug27 ? 00:00:00 [watchdog/0]
root 7 2 0 Aug27 ? 00:00:00 [migration/1]
root 8 2 0 Aug27 ? 00:00:00 [stopper/1]
root 9 2 0 Aug27 ? 00:00:00 [ksoftirqd/1]
root 10 2 0 Aug27 ? 00:00:00 [watchdog/1]
root 11 2 0 Aug27 ? 00:00:07 [events/0]
root 12 2 0 Aug27 ? 00:02:42 [events/1]
root 13 2 0 Aug27 ? 00:00:00 [events/0]
[root@lvs ~]# ps -eFH
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 2 0 0 0 0 1 Aug27 ? 00:00:00 [kthreadd]
root 3 2 0 0 0 0 Aug27 ? 00:00:00 [migration/0]
root 4 2 0 0 0 0 Aug27 ? 00:00:00 [ksoftirqd/0]
root 5 2 0 0 0 0 Aug27 ? 00:00:00 [stopper/0]
root 6 2 0 0 0 0 Aug27 ? 00:00:00 [watchdog/0]
root 7 2 0 0 0 1 Aug27 ? 00:00:00 [migration/1]
root 8 2 0 0 0 1 Aug27 ? 00:00:00 [stopper/1]
root 9 2 0 0 0 1 Aug27 ? 00:00:00 [ksoftirqd/1]
root 10 2 0 0 0 1 Aug27 ? 00:00:00 [watchdog/1]
root 11 2 0 0 0 0 Aug27 ? 00:00:07 [events/0]
root 12 2 0 0 0 1 Aug27 ? 00:02:43 [events/1]
root 13 2 0 0 0 0 Aug27 ? 00:00:00 [events/0]
root 14 2 0 0 0 1 Aug27 ? 00:00:00 [events/1]
root 15 2 0 0 0 0 Aug27 ? 00:00:00 [events_long/0]
root 16 2 0 0 0 1 Aug27 ? 00:00:00 [events_long/1]
自定義顯示pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm,不過需要配合O選項來
[root@lvs ~]# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm
PID TID CLS RTPRIO NI PRI PSR %CPU STAT COMMAND
1 1 TS - 0 19 0 0.0 Ss init
2 2 TS - 0 19 1 0.0 S kthreadd
3 3 FF 99 - 139 0 0.0 S migration/0
4 4 TS - 0 19 0 0.0 S ksoftirqd/0
5 5 FF 99 - 139 0 0.0 S stopper/0
6 6 FF 99 - 139 0 0.0 S watchdog/0
7 7 FF 99 - 139 1 0.0 S migration/1
8 8 FF 99 - 139 1 0.0 S stopper/1
9 9 TS - 0 19 1 0.0 S ksoftirqd/1
10 10 FF 99 - 139 1 0.0 S watchdog/1
11 11 TS - 0 19 0 0.0 S events/0
12 12 TS - 0 19 1 0.1 S events/1
13 13 TS - 0 19 0 0.0 S events/0
14 14 TS - 0 19 1 0.0 S events/1
[root@lvs ~]# ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
STAT EUID RUID TT TPGID SESS PGRP PPID PID %CPU COMMAND
Ss 0 0 ? -1 1 1 0 1 0.0 init
S 0 0 ? -1 0 0 0 2 0.0 kthreadd
S 0 0 ? -1 0 0 2 3 0.0 migration/0
S 0 0 ? -1 0 0 2 4 0.0 ksoftirqd/0
S 0 0 ? -1 0 0 2 5 0.0 stopper/0
S 0 0 ? -1 0 0 2 6 0.0 watchdog/0
S 0 0 ? -1 0 0 2 7 0.0 migration/1
S 0 0 ? -1 0 0 2 8 0.0 stopper/1
S 0 0 ? -1 0 0 2 9 0.0 ksoftirqd/1
S 0 0 ? -1 0 0 2 10 0.0 watchdog/1
S 0 0 ? -1 0 0 2 11 0.0 events/0
S 0 0 ? -1 0 0 2 12 0.1 events/1
S 0 0 ? -1 0 0 2 13 0.0 events/0
S 0 0 ? -1 0 0 2 14 0.0 events/1
S 0 0 ? -1 0 0 2 15 0.0 events_long/0
S 0 0 ? -1 0 0 2 16 0.0 events_long/1
S 0 0 ? -1 0 0 2 17 0.0 events_power_ef
S 0 0 ? -1 0 0 2 18 0.0 events_power_ef
以進程樹顯示系統中的進程如何相互鏈接:
[root@lvs ~]# ps -e --forest
PID TTY TIME CMD
2 ? 00:00:00 kthreadd
3 ? 00:00:00 \_ migration/0
4 ? 00:00:00 \_ ksoftirqd/0
5 ? 00:00:00 \_ stopper/0
6 ? 00:00:00 \_ watchdog/0
7 ? 00:00:00 \_ migration/1
8 ? 00:00:00 \_ stopper/1
9 ? 00:00:00 \_ ksoftirqd/1
10 ? 00:00:00 \_ watchdog/1
11 ? 00:00:07 \_ events/0
12 ? 00:02:44 \_ events/1
13 ? 00:00:00 \_ events/0
14 ? 00:00:00 \_ events/1
15 ? 00:00:00 \_ events_long/0
16 ? 00:00:00 \_ events_long/1
以進程樹顯示指定的進程
[root@lvs ~]# ps -f --forest -C sshd
UID PID PPID C STIME TTY TIME CMD
root 1720 1 0 Aug27 ? 00:00:00 /usr/sbin/sshd
root 2581 1720 0 Aug27 ? 00:00:02 \_ sshd: root@pts/0,pts/1
[root@lvs ~]# ps -ef --forest | grep -v grep | grep sshd
root 1720 1 0 Aug27 ? 00:00:00 /usr/sbin/sshd
root 2581 1720 0 Aug27 ? 00:00:02 \_ sshd: root@pts/0,pts/1
查看進程的PID,PPID,用戶名和命令:
[root@lvs ~]# ps -eo pid,ppid,user,cmd
PID PPID USER CMD
1 0 root /sbin/init
2 0 root [kthreadd]
3 2 root [migration/0]
4 2 root [ksoftirqd/0]
5 2 root [stopper/0]
6 2 root [watchdog/0]
7 2 root [migration/1]
8 2 root [stopper/1]
9 2 root [ksoftirqd/1]
要顯示一個進程的所有線程,將顯示LWP(輕量級進程)以及NLWP(輕量級進程數)列:
[root@lvs ~]# ps -fl -C httpd
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
1 S root 9174 1 0 80 0 - 44381 poll_s 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9249 9174 0 80 0 - 44415 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9250 9174 0 80 0 - 44381 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9251 9174 0 80 0 - 44415 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9252 9174 0 80 0 - 44381 inet_c 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9253 9174 0 80 0 - 44415 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9254 9174 0 80 0 - 44415 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9255 9174 0 80 0 - 44381 sys_se 09:52 ? 00:00:00 /usr/sbin/
5 S apache 9256 9174 0 80 0 - 44381 sys_se 09:52 ? 00:00:00 /usr/sbin/
[root@lvs ~]#
使用其PID查找進程名稱:
[root@lvs ~]# ps -p 8982 -o comm=
su
查找指定進程名所有的所屬PID,在編寫需要從std輸出或文件讀取PID的腳本時這個參數很有用:
[root@lvs ~]# ps -C htttpd,sshd -o pid=
1720
2581
列出所有格式說明符
[root@lvs ~]# ps L
%cpu %CPU
%mem %MEM
_left LLLLLLLL
_left2 L2L2L2L2
_right RRRRRRRR
_right2 R2R2R2R2
_unlimited U
_unlimited2 U2
alarm ALARM
args COMMAND
atime TIME
blocked BLOCKED
bsdstart START
bsdtime TIME
c C
.....
檢查一個進程的執行時間
[root@lvs ~]# ps -eo comm,etime,user| grep httpd
httpd 14:13 root
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
httpd 13:57 apache
查找佔用最多內存和CPU的進程:
[root@lvs ~]# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu |head
PID PPID CMD %MEM %CPU
9374 8983 dd if=/dev/zero of=/dev/nul 0.0 99.2
12 2 [events/1] 0.0 0.1
1 0 /sbin/init 0.0 0.0
2 0 [kthreadd] 0.0 0.0
3 2 [migration/0] 0.0 0.0
4 2 [ksoftirqd/0] 0.0 0.0
5 2 [stopper/0] 0.0 0.0
6 2 [watchdog/0] 0.0 0.0
7 2 [migration/1] 0.0 0.0
用戶定義的格式顯示安全信息
[root@lvs ~]# ps -eo euser,ruser,suser,fuser,f,comm,label
EUSER RUSER SUSER FUSER F COMMAND LABEL
root root root root 4 init system_u:system_r:init_t:s0
root root root root 1 kthreadd system_u:system_r:kernel_t:s0
root root root root 1 migration/0 system_u:system_r:kernel_t:s0
root root root root 1 ksoftirqd/0 system_u:system_r:kernel_t:s0
root root root root 1 stopper/0 system_u:system_r:kernel_t:s0
root root root root 1 watchdog/0 system_u:system_r:kernel_t:s0
root root root root 1 migration/1 system_u:system_r:kernel_t:s0
root root root root 1 stopper/1 system_u:system_r:kernel_t:s0
root root root root 1 ksoftirqd/1 system_u:system_r:kernel_t:s0
root root root root 1 watchdog/1 system_u:system_r:kernel_t:s0
root root root root 1 events/0 system_u:system_r:kernel_t:s0
root root root root 1 events/1 system_u:system_r:kernel_t:s0
root root root root 1 events/0 system_u:system_r:kernel_t:s0
root root root root 1 events/1 system_u:system_r:kernel_t:s0
root root root root 1 events_long/0 system_u:system_r:kernel_t:s0
調整優先級命令
nice 命令
nice [option] [command [arg]...]
沒修改之前sleep 默認nice值爲0
[root@lvs ~]# ps axo pid,comm,ni
.....
12556 sleep 0
12557 ps 0
在命令執行前修改nice值
[cobbler@lvs ~]$ nice -n 10 sleep 100
修改後的nice值
[root@lvs ~]# ps axo pid,comm,ni
12553 sleep 10
12554 ps 0
pgrep 搜索進程命令
最靈活:ps 選項 | 其它命令
支持是用正則表達式
pgrep [options] pattern
-u uid: effective user,生效者
-U uid: real user,真正發起運行命令者
-t terminal: 與指定終端相關的進程
-l: 顯示進程名
-a: 顯示完整格式的進程名
-P pid: 顯示指定進程的子進程
查找以ht開頭的任意進程
[root@lvs ~]# pgrep -l ^ht.*
9174 httpd
9249 httpd
9250 httpd
9251 httpd
9252 httpd
9253 httpd
9254 httpd
9255 httpd
9256 httpd
查找以pass開頭的進程
[root@lvs ~]# pgrep -l ^pass
12687 passwd
查找以哪個終端的進程
[root@lvs ~]# pgrep -lt pts/0
2585 bash
uptime 命令
顯示當前時間,系統已啓動的時間、當前上線人數,系統平均負載(1、5、10分鐘的平均負載,一般不會超過1)
系統平均負載:
指在特定時間間隔內運行隊列中的平均進程數
[root@lvs ~]# uptime
11:08:21 up 1 day, 2:33, 4 users, load average: 0.00, 0.00, 0.01
top 實時動態地查看系統的整體運行情況
top:有許多內置命令:
排序:
P:以佔據的CPU百分比,%CPU
M:佔據內存百分比,%MEM
T:累積佔據CPU時長,TIME+
首部信息顯示:
uptime信息:l命令
tasks及cpu信息:t命令
cpu分別顯示:1 (數字)
memory信息:m命令
退出命令:q
修改刷新時間間隔:s
終止指定進程:k
保存文件:W
欄位信息簡介:
us:用戶空間
sy:內核空間
ni:調整nice時間
id:空閒
wa:等待IO時間
hi:硬中斷
si:軟中斷(模式切換)
st:虛擬機偷走的時間
選項:
-d # 指定刷新時間間隔,默認爲3秒
-b 全部顯示所有進程
-n # 刷新多少次後退出
-H 線程模式,示例:top -H -p `pidof mysqld`
指定刷新時間爲1秒,默認爲3秒,顯示全部進程
[root@lvs ~]# top -d 1 -b
內存管理和查看命令
內存空間使用狀態
free [option]
選項:
-b 以字節爲單位
-m 以MB爲單位
-g 以GB爲單位
-h 易讀格式
-o 不顯示-/+buffers/cache行
-t 顯示RAM + swap的總和
-s n 刷新間隔爲n秒
-c n 刷新n次後即退出
# 能易讀格式 -h
[root@lvs ~]# free -h
total used free shared buffers cached
Mem: 1.8G 1.7G 145M 332K 18M 1.5G
-/+ buffers/cache: 163M 1.7G
Swap: 3.0G 0B 3.0G
[root@lvs ~]#
# -b 以字節爲單位
[root@lvs ~]# free -b
total used free shared buffers cached
Mem: 1950887936 1798332416 152555520 339968 19484672 1607409664
-/+ buffers/cache: 171438080 1779449856
Swap: 3246387200 0 3246387200
[root@lvs ~]#
指定刷新間隔1秒
[root@lvs ~]# free -h -s 1
total used free shared buffers cached
Mem: 1.8G 1.7G 145M 332K 18M 1.5G
-/+ buffers/cache: 163M 1.7G
Swap: 3.0G 0B 3.0G
指定刷新次數就退出
[root@lvs ~]# free -h -s 2 -c 2
total used free shared buffers cached
Mem: 1.8G 1.7G 145M 332K 18M 1.5G
-/+ buffers/cache: 163M 1.7G
Swap: 3.0G 0B 3.0G
total used free shared buffers cached
Mem: 1.8G 1.7G 145M 332K 18M 1.5G
-/+ buffers/cache: 163M 1.7G
Swap: 3.0G 0B 3.0G
[root@lvs ~]#
vmstat命令: vmstat [options] [delay [count]]
procs:
r:可運行(正運行或等待運行)進程的個數,和核心數有關
b:處於不可中斷睡眠態的進程個數(被阻塞的隊列的長度)
memory:
swpd: 交換內存的使用總量
free:空閒物理內存總量
buffer:用於buffer的內存總量
cache:用於cache的內存總量
swap:
si:從磁盤交換進內存的數據速率(kb/s)
so:從內存交換至磁盤的數據速率(kb/s)
io:
bi:從塊設備讀入數據到系統的速率(kb/s)
bo: 保存數據至塊設備的速率
system:
in: interrupts 中斷速率,包括時鐘
cs: context switch 進程切換速率
cpu:
us:Time spent running non-kernel code
sy: Time spent running kernel code
id: Time spent idle. Linux 2.5.41前,包括IO-wait time.
wa: Time spent waiting for IO. 2.5.41前,包括in idle.
st: Time stolen from a virtual machine. 2.6.11前, unknown.
統計內存數據
[root@lvs ~]# vmstat -s
1905164 total memory
1756192 used memory
126336 active memory
1488324 inactive memory
148972 free memory
19204 buffer memory
1569768 swap cache
3170300 total swap
0 used swap
3170300 free swap
37687 non-nice user cpu ticks
50 nice user cpu ticks
98054 system cpu ticks
21566338 idle cpu ticks
2842 IO-wait cpu ticks
37 IRQ cpu ticks
7100 softirq cpu ticks
0 stolen cpu ticks
2362662 pages paged in
2276586 pages paged out
0 pages swapped in
0 pages swapped out
3285297 interrupts
2187566 CPU context switches
1535330071 boot time
13649 forks
iostat:統計CPU和設備IO信息
示例:iostat 1 10
[root@lvs ~]# iostat 1 10
Linux 2.6.32-754.el6.x86_64 (lvs) 08/28/2018 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.17 0.00 0.48 0.01 0.00 99.33
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
scd0 0.00 0.01 0.00 572 0
sda 0.46 43.41 41.87 4721080 4553236
dm-0 0.00 0.02 0.01 1786 1136
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.50 0.00 0.00 99.50
pmap [options] pid [...]
-x: 顯示詳細格式的信息
示例:pmap 12757
另外一種實現:
cat /proc/PID/maps
[root@lvs ~]# pmap -x 12757
12757: vim test
Address Kbytes RSS Dirty Mode Mapping
0000000000400000 2140 1200 0 r-x-- vim
0000000000817000 92 80 60 rw--- vim
000000000082e000 44 36 36 rw--- [ anon ]
0000000000a2d000 40 8 0 rw--- vim
00000000012c9000 2160 2024 2024 rw--- [ anon ]
00000034cbe00000 128 116 0 r-x-- ld-2.12.so
00000034cc020000 4 4 4 r---- ld-2.12.so
00000034cc021000 4 4 4 rw--- ld-2.12.so
00000034cc022000 4 4 4 rw--- [ anon ]
00000034cc200000 8 8 0 r-x-- libdl-2.12.so
00000034cc202000 2048 0 0 ----- libdl-2.12.so
00000034cc402000 4 4 4 r---- libdl-2.12.so
00000034cc403000 4 4 4 rw--- libdl-2.12.so
00000034cc600000 1580 520 0 r-x-- libc-2.12.so
.....
[root@lvs ~]# cat /proc/12757/maps
00400000-00617000 r-xp 00000000 08:02 789328 /usr/bin/vim
00817000-0082e000 rw-p 00217000 08:02 789328 /usr/bin/vim
0082e000-00839000 rw-p 00000000 00:00 0
00a2d000-00a37000 rw-p 0022d000 08:02 789328 /usr/bin/vim
012c9000-014e5000 rw-p 00000000 00:00 0 [heap]
34cbe00000-34cbe20000 r-xp 00000000 08:02 653214 /lib64/ld-2.12.so
34cc020000-34cc021000 r--p 00020000 08:02 653214 /lib64/ld-2.12.so
34cc021000-34cc022000 rw-p 00021000 08:02 653214 /lib64/ld-2.12.so
34cc022000-34cc023000 rw-p 00000000 00:00 0
34cc200000-34cc202000 r-xp 00000000 08:02 653217 /lib64/libdl-2.12.so
EPEL源監控工具
htop命令: top的升級版,功能更強大
選項:
-d #: 指定延遲時間;
-u UserName: 僅顯示指定用戶的進程
-s COLUME: 以指定字段進行排序
子命令:
s: 跟蹤選定進程的系統調用
l: 顯示選定進程打開的文件列表
a:將選定的進程綁定至某指定CPU核心
t: 顯示進程樹
glances命令:
語法: glances [-bdehmnrsvyz1] [-B bind] [-c server] [-C conffile] [-p port] [-P password] [--password] [-t refresh] [-f file] [-o output]
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
常用選項:
-b: 以Byte爲單位顯示網卡數據速率
-d: 關閉磁盤I/O模塊
-f /path/to/somefile: 設定輸入文件位置
-o {HTML|CSV}:輸出格式
-m: 禁用mount模塊
-n: 禁用網絡模塊
-t #: 延遲時間間隔
-1:每個CPU的相關數據單獨顯示
glances是以C/S模式運行的
服務器模式:
glances -s -B IPADDR
也可以不用指定監聽哪個地址 默認全部監聽
IPADDR: 指明監聽的本機哪個地址
服務器上監聽192.168.50.130這個地址上
[root@lvs ~]# glances -s -B 192.168.50.130
Glances server is running on 192.168.50.130:61209
客戶端
[root@centos6 ~]#glances -c 192.168.50.130
lvs (CentOS 6.10 64bit / Linux 2.6.32-754.el6.x86_64) Uptime: 1 day, 6:48:53
CPU 3.1% Load 2-core Mem 9.3% active: 137M Swap 0.0%
user: 1.1% 1 min: 0.17 total: 1.82G inactive: 1.39G total: 3.02G
system: 2.0% 5 min: 0.42 used: 174M buffers: 20.7M used: 0
idle: 96.9% 15 min: 0.22 free: 1.65G cached: 1.47G free: 3.02G
Network Rx/s Tx/s Tasks 131 (200 thr), 1 run, 130 slp, 0 oth
eth0 3Kb 113Kb
eth1 0b 0b VIRT RES CPU% MEM% PID NAME
lo 0b 0b 205M 12M 4.7 0.6 13829 glances
0 0 0.3 0.0 12 events/1
Disk I/O In/s Out/s 19M 2M 0.0 0.1 1 /sbin/init
dm-0 0 0 0 0 0.0 0.0 2 kthreadd
sda1 0 0 0 0 0.0 0.0 3 migration/0
sda2 0 0 0 0 0.0 0.0 4 ksoftirqd/0
sda3 0 0 0 0 0.0 0.0 5 stopper/0
sda4 0 0 0 0 0.0 0.0 6 watchdog/0
sda5 0 0 0 0 0.0 0.0 7 migration/1
0 0 0.0 0.0 8 stopper/1
Mount Used Total 0 0 0.0 0.0 9 ksoftirqd/1
/ 3.29G 19.1G 0 0 0.0 0.0 10 watchdog/1
Connected to 192.168.50.130 | Press 'h' for help 2018-08-28 05:38:40
dstat 命令: 系統資源統計,代替vmstat,iostat
dstat [-afv] [options..] [delay [count]]
-c: 顯示cpu相關信息
-C #,#,...,total
-d: 顯示disk相關信息
-D total,sda,sdb,...
-g:顯示page相關統計數據
-m: 顯示memory相關統計數據
-n: 顯示network相關統計數據
-p: 顯示process相關統計數據
-r: 顯示io請求相關的統計數據
-s: 顯示swapped相關的統計數據
--tcp
--udp
--unix
--raw
--socket
--ipc
--top-cpu:顯示最佔用CPU的進程
--top-io: 顯示最佔用io的進程
--top-mem: 顯示最佔用內存的進程
--top-latency: 顯示延遲最大的進程
[root@lvs ~]# dstat
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
0 1 99 0 0 0| 21k 21k| 0 0 | 0 0 | 48 20
0 1 99 0 0 0| 0 0 | 41M 41M| 0 0 | 10k 44
0 0 99 0 0 1| 0 0 | 41M 41M| 0 0 | 10k 44
0 0 100 0 0 0| 0 0 | 41M 41M| 0 0 | 10k 49
0 0 100 0 0 0| 0 1024k| 41M 41M| 0 0 | 10k 51
1 0 99 0 0 0| 0 80k| 39M 39M| 0 0 |9416 50
0 1 99 0 0 1| 0 0 | 39M 38M| 0 0 |8684 43
0 0 100 0 0 0| 0 0 | 39M 39M| 0 0 |9262 43
0 0 100 0 0 0| 0 0 | 41M 41M| 0 0 | 10k 42
0 0 99 0 0 1| 0 192k| 41M 41M| 0 0 | 10k 47
0 0 100 0 0 0| 0 0 | 38M 38M| 0 0 |9070 41
0 0 99 0 0 1| 0 0 | 40M 40M| 0 0 |9825 41
0 0 100 0 0 0| 0 0 | 40M 40M| 0 0 | 10k 41
0 0 100 0 0 0| 0 0 | 41M 41M| 0 0 |9975 43
0 1 99 0 0 0| 0 0 | 41M 41M| 0 0 | 10k 48
0 0 100 0 0 0| 0 0 | 40M 40M| 0 0 |9576 47
lsof: list open files查看當前系統文件的工具。
命令參數:
-a:列出打開文件存在的進程
-c<進程名>:列出指定進程所打開的文件
-g:列出GID號進程詳情
-d<文件號>:列出佔用該文件號的進程
+d<目錄>:列出目錄下被打開的文件
+D<目錄>:遞歸列出目錄下被打開的文件
-n<目錄>:列出使用NFS的文件
-i<條件>:列出符合條件的進程(4、6、協議、:端口、 @ip )
-p<進程號>:列出指定進程號所打開的文件
-u:列出UID號進程詳情
-h:顯示幫助信息
-v:顯示版本信息。
-n: 不反向解析網絡名字
進程管理
查看由登錄用戶啓動而非系統啓動的進程
[root@lvs ~]# lsof /dev/pts/0
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 2585 root 0u CHR 136,0 0t0 3 /dev/pts/0
bash 2585 root 1u CHR 136,0 0t0 3 /dev/pts/0
bash 2585 root 2u CHR 136,0 0t0 3 /dev/pts/0
bash 2585 root 255u CHR 136,0 0t0 3 /dev/pts/0
glances 13829 root 0u CHR 136,0 0t0 3 /dev/pts/0
glances 13829 root 1u CHR 136,0 0t0 3 /dev/pts/0
glances 13829 root 2u CHR 136,0 0t0 3 /dev/pts/0
指定進程號,查看該進程打開的文件;
[root@lvs ~]# lsof -p 8967
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 8967 root cwd DIR 8,2 4096 1044483 /root
bash 8967 root rtd DIR 8,2 4096 2 /
bash 8967 root txt REG 8,2 942200 915863 /bin/bash
bash 8967 root mem REG 8,2 161776 653214 /lib64/ld-2.12.so
bash 8967 root mem REG 8,2 23088 653217 /lib64/libdl-2.12.so
bash 8967 root mem REG 8,2 1930416 653215 /lib64/libc-2.12.so
bash 8967 root mem REG 8,2 134792 653253 /lib64/libtinfo.so.5.7
bash 8967 root mem REG 8,2 99174448 784755 /usr/lib/locale/locale-archive
bash 8967 root mem REG 8,2 66432 652834 /lib64/libnss_files-2.12.so
bash 8967 root mem REG 8,2 26060 785015 /usr/lib64/gconv/gconv-modules.cache
....
文件管理
指定程序打開的文件
[root@lvs ~]# lsof -c httpd
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 9174 root cwd DIR 8,2 4096 2 /
httpd 9174 root rtd DIR 8,2 4096 2 /
httpd 9174 root txt REG 8,2 367136 798455 /usr/sbin/httpd
httpd 9174 root mem REG 8,2 9488 798371 /usr/lib64/apr-util-1/apr_ldap-1.so
httpd 9174 root mem REG 8,2 27896 652832 /lib64/libnss_dns-2.12.so
httpd 9174 root mem REG 8,2 66432 652834 /lib64/libnss_files-2.12.so
httpd 9174 root mem REG 8,2 10416 798451 /usr/lib64/httpd/modules/mod_version.so
httpd 9174 root mem REG 8,2 27312 798410 /usr/lib64/httpd/modules/mod_cgi.so
httpd 9174 root mem REG 8,2 22992 798417 /usr/lib64/httpd/modules/mod_disk_cache.so
httpd 9174 root mem REG 8,2 10384 798447 /usr/lib64/httpd/modules/mod_suexec.so
指定用戶打開的文件
[root@lvs ~]# lsof -u root | more
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 8,2 4096 2 /
init 1 root rtd DIR 8,2 4096 2 /
init 1 root txt REG 8,2 150352 1051071 /sbin/init
init 1 root mem REG 8,2 66432 652834 /lib64/libns
s_files-2.12.so
init 1 root mem REG 8,2 1930416 653215 /lib64/libc-
2.12.so
init 1 root DEL REG 8,2 653235 /lib64/libgc
c_s-4.4.7-20120601.so.1.#prelink#.Ek7K1Q
init 1 root mem REG 8,2 47760 653223 /lib64/librt
-2.12.so
init 1 root mem REG 8,2 146592 653222 /lib64/libpt
hread-2.12.so
init 1 root mem REG 8,2 268240 652925 /lib64/libdb
us-1.so.3.4.0
init 1 root mem REG 8,2 39896 653003 /lib64/libni
h-dbus.so.1.0.0
init 1 root mem REG 8,2 106016 653005 /lib64/libni
h.so.1.0.0
init 1 root mem REG 8,2 161776 653214 /lib64/ld-2.
12.so
init 1 root 0u CHR 1,3 0t0 4601 /dev/null
.....
指定目錄下被打開的文件
參數+D爲遞歸列出目錄下被打開的文件,參數+d爲列出目錄下被打開的文件
[root@lvs ~]# lsof +D /var/log/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
auditd 1412 root 5w REG 8,2 514162 392909 /var/log/audit/audit.log
rsyslogd 1434 root 1w REG 8,2 163242 392981 /var/log/messages
rsyslogd 1434 root 2w REG 8,2 1526 392982 /var/log/secure
rsyslogd 1434 root 4w REG 8,2 335 392980 /var/log/maillog
rsyslogd 1434 root 5w REG 8,2 29024 392979 /var/log/cron
rsyslogd 1434 root 6w REG 8,2 2628 391700 /var/log/boot.log
console-k 1891 root 9w REG 8,2 2053 391701 /var/log/ConsoleKit/history
httpd 9174 root 2w REG 8,2 709 393074 /var/log/httpd/error_log
httpd 9174 root 7w REG 8,2 859 393083 /var/log/httpd/access_log
httpd 9249 apache 2w REG 8,2 709 393074 /var/log/httpd/error_log
[root@lvs ~]# lsof +d /var/log/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd 1434 root 1w REG 8,2 163242 392981 /var/log/messages
rsyslogd 1434 root 2w REG 8,2 1526 392982 /var/log/secure
rsyslogd 1434 root 4w REG 8,2 335 392980 /var/log/maillog
rsyslogd 1434 root 5w REG 8,2 29024 392979 /var/log/cron
rsyslogd 1434 root 6w REG 8,2 2628 391700 /var/log/boot.log
網絡管理
查看所有網絡連接
[root@lvs ~]# lsof -i -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1720 root 3u IPv4 14010 0t0 TCP *:ssh (LISTEN)
sshd 1720 root 4u IPv6 14015 0t0 TCP *:ssh (LISTEN)
master 1799 root 12u IPv4 14220 0t0 TCP 127.0.0.1:smtp (LISTEN)
master 1799 root 13u IPv6 14221 0t0 TCP [::1]:smtp (LISTEN)
sshd 2581 root 3u IPv4 16010 0t0 TCP 192.168.50.130:ssh->192.168.50.1:49430 (ESTABLISHED)
dhclient 5549 root 6u IPv4 22052 0t0 UDP *:bootpc
dhclient 5670 root 6u IPv4 22186 0t0 UDP *:bootpc
httpd 9174 root 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9249 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9250 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9251 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9252 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9253 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9254 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9255 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9256 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
ssh 13753 root 3r IPv4 52325 0t0 TCP 192.168.50.130:35156->192.168.50.128:ssh (ESTABLISHED)
glances 13829 root 3u IPv4 52565 0t0 TCP 192.168.50.130:61209 (LISTEN)
# 查看指定IP網絡連接
[root@lvs ~]# lsof [email protected]
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 2581 root 3u IPv4 16010 0t0 TCP 192.168.50.130:ssh->192.168.50.1:49430 (ESTABLISHED)
ssh 13753 root 3r IPv4 52325 0t0 TCP 192.168.50.130:35156->192.168.50.128:ssh (ESTABLISHED)
glances 13829 root 3u IPv4 52565 0t0 TCP 192.168.50.130:61209 (LISTEN)
查看指定端口連接情況
[root@lvs ~]# lsof -i :80 -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 9174 root 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9249 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9250 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9251 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9252 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9253 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9254 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9255 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
httpd 9256 apache 4u IPv6 29668 0t0 TCP *:http (LISTEN)
查看指定進程打開的網絡連接數
-i、-a、-p等,-i查看網絡連接情況,-a查看存在的進程,-p指定進程
[root@lvs ~]# lsof -i -n -a -p 13829
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
glances 13829 root 3u IPv4 52565 0t0 TCP 192.168.50.130:61209 (LISTEN)
查看指定狀態的網絡連接
-n:no host names, -P:no port names,-i TCP指定協議,-s指定協議狀態通過多個參數可以清晰的查看網絡連接情況、協議連接情況等
[root@lvs ~]# lsof -n -P -i TCP -s TCP:ESTABLISHED
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 2581 root 3u IPv4 16010 0t0 TCP 192.168.50.130:22->192.168.50.1:49430 (ESTABLISHED)
ssh 13753 root 3r IPv4 52325 0t0 TCP 192.168.50.130:35156->192.168.50.128:22 (ESTABLISHED)
glances 13829 root 4u IPv4 55271 0t0 TCP 192.168.50.130:61209->192.168.50.128:41386 (ESTABLISHED)
lsof 還可以用來恢復刪除的文件,不過不要重啓系統,否則就沒戲了
1、查找/var/log/messages文件
[root@lvs ~]# lsof | grep /var/log/messages
rsyslogd 1434 root 1w REG 8,2 163779 392981 /var/log/messages
2、刪除/var/log/messages文件
[root@lvs ~]# rm -rf /var/log/messages
3、在用查找命令發現/var/log/messages被標記爲deleted
[root@lvs ~]# lsof | grep /var/log/messages
rsyslogd 1434 root 1w REG 8,2 163779 392981 /var/log/messages (deleted)
4、找到被刪的文件的進程號,到/proc/下指定進程號目錄下
[root@lvs ~]# cat /proc/1434/fd/1
Aug 28 01:58:09 lvs dhclient[5670]: DHCPACK from 192.168.112.254 (xid=0x119e82b7)
Aug 28 01:58:11 lvs dhclient[5670]: bound to 192.168.112.128 -- renewal in 849 seconds.
Aug 28 02:05:48 lvs dhclient[5549]: DHCPREQUEST on eth0 to 192.168.50.254 port 67 (xid=0x57ab2d56)
Aug 28 02:05:48 lvs dhclient[5549]: DHCPACK from 192.168.50.254 (xid=0x57ab2d56)
Aug 28 02:05:50 lvs dhclient[5549]: bound to 192.168.50.130 -- renewal in 880 seconds.
Aug 28 02:12:20 lvs dhclient[5670]: DHCPREQUEST on eth1 to 192.168.112.254 port 67 (xid=0x119e82b7)
5、重定向到/var/log/messages裏面
[root@lvs ~]# cat /proc/1434/fd/1 > /var/log/messages
6、對比大小,恢復完成
[root@lvs ~]# ll /var/log/messages
-rw-r--r--. 1 root root 163779 Aug 28 16:02 /var/log/messages
kill命令:
向進程發送控制信號,以實現對進程管理,每個信號對應一個數字,信號名稱以SIG開頭(可省略),不區分大小寫
顯示當前系統可用信號: kill –l,trap -l
[root@lvs ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[root@lvs ~]# trap -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
常用信號:man 7 signal
1) SIGHUP: 無須關閉進程而讓其重讀配置文件
2) SIGINT: 中止正在運行的進程;相當於Ctrl+c
3) SIGQUIT:相當於ctrl+\
9) SIGKILL: 強制殺死正在運行的進程
15) SIGTERM:終止正在運行的進程
18) SIGCONT:繼續運行
19) SIGSTOP:後臺休眠
指定信號的方法:
(1) 信號的數字標識:1, 2, 9
(2) 信號完整名稱:SIGHUP
(3) 信號的簡寫名稱:HUP
按PID:kill [-SIGNAL] pid …
kill –n SIGNAL pid;kill –s SIGNAL pid
按名稱:killall [-SIGNAL] comm…
按模式:pkill [options] pattern
-SIGNAL
-u uid: effective user,生效者
-U uid: real user,真正發起運行命令者
-t terminal: 與指定終端相關的進程
-l: 顯示進程名(pgrep可用)
-a: 顯示完整格式的進程名(pgrep可用)
-P pid: 顯示指定進程的子進程