?
?
?
?
?
主題Linux進程管理之ps工具的使用
?
?
?
一ps工具的介紹
?
?
ps: process state? 進程狀態
ps - report a snapshot of the current processes
Linux系統各進程的相關信息均保存在/proc/PID目錄下的各文件中
?
?
默認顯示的內容很少
[root@centos65 ~]# psPID TTY TIME CMD2018 pts/0 00:00:00 bash 2656 pts/0 00:00:00 ps
?
?
?
?
[root@centos72 ~]# psPID TTY TIME CMD1928 pts/0 00:00:01 bash 101855 pts/0 00:00:00 ps
?
?
?
?
?
?
顯示當前終端的進程
[root@centos65 ~]# tty
/dev/pts/0
?
?
?
?
?
[root@centos65 ~]# sleep 10 &
[1] 2678 [root@centos65 ~]# ps PID TTY TIME CMD 2018 pts/0 00:00:00 bash 2678 pts/0 00:00:00 sleep 2679 pts/0 00:00:00 ps
?
?
?
?
?
?
?
?
二ps支持的選項
?
?
ps [OPTION]...
支持三種選項:
UNIX選項 如-A
BSD選項? 如a
GNU選項 如--help
選項:默認顯示當前終端中的進程
? a 選項包括所有終端中的進程
? x 選項包括不鏈接終端的進程
? u 選項顯示進程所有者的信息
? f 選項顯示進程樹,相當于 --forest
? k| --sort 屬性 對屬性 排序,屬性前加- 表示倒序
? o 屬性… 選項顯示定制的信息 pid、cmd、%cpu、%mem
? L 顯示支持的屬性列表
?
?
?
?
?
?
?
(一)BSD風格的選項(最常用)
?
(1)a顯示所有終端中的進程
?
PID是進程的標識號。
TTY是進程所屬的終端控制臺。
TIME列是進程所使用的總的CPU時間。
CMD列是正在執行的命令行。
[root@centos65 ~]# ps aPID TTY STAT TIME COMMAND1866 tty1 Ss+ 0:00 /sbin/mingetty /dev/tty1 1868 tty2 Ss+ 0:00 /sbin/mingetty /dev/tty2 1871 tty3 Ss+ 0:00 /sbin/mingetty /dev/tty3 1873 tty4 Ss+ 0:00 /sbin/mingetty /dev/tty4 1875 tty5 Ss+ 0:00 /sbin/mingetty /dev/tty5 1877 tty6 Ss+ 0:00 /sbin/mingetty /dev/tty6 2018 pts/0 Ss 0:00 -bash 2455 pts/1 Ss 0:00 -bash 2496 pts/1 S 0:00 su - wang 2497 pts/1 S+ 0:00 -bash 2752 pts/0 R+ 0:00 ps a
?
?
?
?
[root@centos65 ~]# su - wang
[wang@centos65 ~]$ ls
[wang@centos65 ~]$ vim /etc/fstab
[wang@centos65 ~]$ tty /dev/pts/1
?
?
?
?
?
?
?
(2)x顯示和終端無關的進程
?
不需要用戶賬號登錄就可以運行,機器啟動就運行起來了
[root@centos72 ~]# ps ax | headPID TTY STAT TIME COMMAND1 ? Ss 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 2 ? S 0:00 [kthreadd] 3 ? S 0:00 [ksoftirqd/0] 5 ? S< 0:00 [kworker/0:0H] 7 ? S 0:00 [migration/0] 8 ? S 0:00 [rcu_bh] 9 ? R 0:00 [rcu_sched] 10 ? S< 0:00 [lru-add-drain] 11 ? S 0:00 [watchdog/0] [root@centos72 ~]# ps ax | tail 882 ? Ssl 0:00 /usr/sbin/rsyslogd -n 906 ? Ss 0:00 sshd: root@pts/0 967 ? Ss 0:00 /usr/libexec/postfix/master -w 968 ? S 0:00 pickup -l -t unix -u 969 ? S 0:00 qmgr -l -t unix -u 1142 pts/0 Ss 0:00 -bash 1179 ? S 0:00 [kworker/0:0] 1191 ? S 0:00 [kworker/0:1] 1195 pts/0 R+ 0:00 ps ax 1196 pts/0 D+ 0:00 -bash
?
?
?
?
?
?
?
?
?
?
(3)顯示所有和終端有關的進程用戶信息
?
USER:該進程屬于的用戶。
PID:該進程的進程號。
%CPU:該進程使用掉的CPU資源百分比,CPU利用率
?
%MEM:該進程所占用的物理內存百分比,內存利用率
VSZ:該進程使用掉的虛擬內存量(單位為Kbytes)
?
VSZ虛擬內存占用(操作系統給應用程序的內存大小,包括物理內存和swap內存)
vsz VSZ virtual memory size of the process in KiB (1024-byte units).
進程的虛擬內存大小為KiB(1024字節單位)。Device mappings are currently excluded; this is subject to change. (alias vsize).
目前不包括設備映射;這一點可能會改變。(別名vsize)。
?
?
?
?
?
RSS:Resident Set Size,該進程占用的固定的內存量(單位為Kbytes)。
?
RSS真正的物理內存
TTY:該進程是在哪個終端機上面運作的,若與終端機無關,則顯示“?”,
?
另外,tty1-tty6是本機上面的登入者進程,若為pts/0等,則表示為由網絡連接進主機的進程。
STAT:該進程目前的狀態,主要的狀態:
R:正在運行,或者是可以運行。
S:正在中斷睡眠中,可以由某些信號(signal)喚醒。
D:不可中斷睡眠。
T:正在偵測或者是停止了。
Z:已經終止,但是其父進程無法正常終止它,從而變成zombic(僵尸)進程的狀態。
+:前臺進程。
1:多線程進程。
N:低優先級進程。
<:高優先級進程。
s:進程領導者。
L:已將頁面鎖定到內存中。
START:該進程被觸發啟動的時間。
TIME:該進程實際使用CPU運作的時間。也就是TIME時間片的累計值
COMMAND:該進程的實際命令。
?
?
?
[root@centos72 ~]# ps au
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 540 0.0 0.0 110088 856 tty1 Ss+ Jul08 0:00 /sbin/agetty --noclear tty1 linux root 1928 0.0 0.2 116092 2944 pts/0 Ss Jul09 0:01 -bash root 5073 0.0 0.1 155324 1868 pts/0 R+ 12:13 0:00 ps au root 103122 0.0 0.2 115832 2464 pts/1 Ss+ 01:00 0:00 -bash
?
?
?
?
?
?
?
?
(4)顯示所有進程用戶信息
?
組合選項使用最多的,因為顯示的信息很詳細
?
[root@centos72 ~]# ps axu | tail
root 882 0.0 0.8 214424 8664 ? Ssl 16:56 0:00 /usr/sbin/rsyslogd -n root 906 0.0 0.5 154588 5384 ? Ss 16:56 0:00 sshd: root@pts/0 root 967 0.0 0.2 89620 2080 ? Ss 16:56 0:00 /usr/libexec/postfix/master -w postfix 968 0.0 0.4 89724 4052 ? S 16:56 0:00 pickup -l -t unix -u postfix 969 0.0 0.4 89792 4080 ? S 16:56 0:00 qmgr -l -t unix -u root 1142 0.0 0.2 115968 2692 pts/0 Ss 16:56 0:00 -bash root 1179 0.0 0.0 0 0 ? S 17:01 0:00 [kworker/0:0] root 1191 0.0 0.0 0 0 ? S 17:06 0:00 [kworker/0:1] root 1197 0.0 0.1 155324 1860 pts/0 R+ 17:08 0:00 ps axu root 1198 0.0 0.0 107984 652 pts/0 R+ 17:08 0:00 tail [root@centos72 ~]# ps axu | head USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.1 0.6 127780 6436 ? Ss 16:55 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0.0 0.0 0 0 ? S 16:55 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 16:55 0:00 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 16:55 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S 16:55 0:00 [migration/0] root 8 0.0 0.0 0 0 ? S 16:55 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? R 16:55 0:00 [rcu_sched] root 10 0.0 0.0 0 0 ? S< 16:55 0:00 [lru-add-drain] root 11 0.0 0.0 0 0 ? S 16:55 0:00 [watchdog/0]
?
?
?
?
?
?
?
(5)f 選項顯示進程樹,相當于 --forest
[root@centos72 ~]# ps auxf | head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S Jul08 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S Jul08 0:01 \_ [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< Jul08 0:00 \_ [kworker/0:0H] root 7 0.0 0.0 0 0 ? S Jul08 0:00 \_ [migration/0] root 8 0.0 0.0 0 0 ? S Jul08 0:00 \_ [rcu_bh] root 9 0.0 0.0 0 0 ? R Jul08 0:09 \_ [rcu_sched] root 10 0.0 0.0 0 0 ? S< Jul08 0:00 \_ [lru-add-drain] root 11 0.0 0.0 0 0 ? S Jul08 0:02 \_ [watchdog/0] root 13 0.0 0.0 0 0 ? S Jul08 0:00 \_ [kdevtmpfs]
?
?
?
?
?
?
顯示了進程的父子關系
進程必須放到樹上的某個分支
[root@centos65 ~]# ps auxf | grep apache
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ root 2950 0.0 0.0 103324 836 pts/0 S+ 15:00 0:00 | \_ grep --color=auto apache apache 2300 0.0 0.5 521880 5344 ? Sl 14:20 0:00 \_ /usr/sbin/httpd.worker apache 2301 0.0 0.5 521880 5336 ? Sl 14:20 0:00 \_ /usr/sbin/httpd.worker apache 2303 0.0 0.5 521880 5340 ? Sl 14:20 0:00 \_ /usr/sbin/httpd.worker
?
?
?
?
?
[root@centos72 ~]# ps auxf | grep sshd
root 862 0.0 0.4 112796 4336 ? Ss Jul08 0:00 /usr/sbin/sshd -D root 1924 0.0 0.5 154872 5756 ? Ss Jul09 0:03 \_ sshd: root@pts/0 root 103114 0.0 0.5 154588 5388 ? Ss Jul14 0:00 \_ sshd: root@pts/1 root 86615 0.0 0.0 112704 972 pts/1 S+ 16:18 0:00 \_ grep --color=auto sshd
?
?
?
?
?
?
(6)o自定義輸出指定的字段
[root@centos65 ~]# ps axo pid,cmd,%cpu k cmd | headPID CMD %CPU1761 abrt-dump-oops -d /var/spoo 0.0 1751 /usr/sbin/abrtd 0.0 1379 /usr/sbin/acpid 0.0 37 [aio/0] 0.0 14 [async/mgr] 0.0 23 [ata_aux] 0.0 24 [ata_sff/0] 0.0 1808 /usr/sbin/atd 0.0 1203 auditd 0.0
?
?
?
?
?
打開另外一個終端
[root@centos72 ~]# id wang
uid=1000(wang) gid=1000(wang) groups=1000(wang) [root@centos72 ~]# su - wang Last login: Thu May 9 16:22:21 CST 2019 on pts/1 [wang@centos72 ~]$ passwd Changing password for user wang. Changing password for wang. (current) UNIX password:
?
?
?
?
uid,euid都表示有效用戶是root,真正的用戶是wang
因為這是因為普通用戶具有suid權限
[root@centos72 ~]# ps axo pid,cmd,ni,%cpu,uid,euid,ruid | tail1252 [kworker/0:2] 0 0.0 0 0 0 1269 [kworker/0:0] 0 0.0 0 0 0 1270 [kworker/1:0] 0 0.0 0 0 0 1273 [kworker/1:2] 0 0.0 0 0 0 1276 su - wang 0 0.0 0 0 0 1277 -bash 0 0.0 1000 1000 1000 1300 passwd 0 0.1 0 0 1000 1305 [kworker/0:1] 0 0.0 0 0 0 1308 ps axo pid,cmd,ni,%cpu,uid, 0 0.0 0 0 0 1309 tail 0 0.0 0 0 0
?
?
?
?
?
?
(7)k按照指定字段排序
按照內存排序,在6上不支持
如果使用圖形化界面,切換到命令行就會節約很大的內存
[root@centos65 ~]# ps axo pid,cmd,%cpu,%mem k %mem | tail2300 /usr/sbin/httpd.worker 0.0 0.52301 /usr/sbin/httpd.worker 0.0 0.5 2303 /usr/sbin/httpd.worker 0.0 0.5 2451 sshd: root@pts/1 0.0 0.4 2455 -bash 0.0 0.4 2496 su - wang 0.0 0.1 2497 -bash 0.0 0.4 3156 sleep 60 0.0 0.0 3160 ps axo pid,cmd,%cpu,%mem k 0.0 0.1 3161 tail 0.0 0.0
?
?
?
?
?
[root@centos72 ~]# ps axo pid,cmd,%cpu,%mem k %mem | tail1507 sshd: root@pts/0 0.0 0.5 1531 sshd: root@pts/1 0.0 0.5 766 /usr/sbin/rsyslogd -n 0.0 0.5 526 /usr/bin/VGAuthService -s 0.0 0.6 527 /usr/bin/vmtoolsd 0.0 0.6 372 /usr/lib/systemd/systemd-ud 0.0 0.6 1 /usr/lib/systemd/systemd -- 0.0 0.6 539 /usr/sbin/NetworkManager -- 0.0 0.9 541 /usr/lib/polkit-1/polkitd - 0.0 1.1 765 /usr/bin/python -Es /usr/sb 0.0 1.9
?
?
?
?
?
?
內存的使用情況:
[root@centos72 ~]# free -htotal used free shared buff/cache available
Mem: 974M 98M 716M 7.7M 159M 705M Swap: 2.0G 0B 2.0G
?
?
?
?
?
?
?
?
(8)L 顯示支持的屬性列表
?
?
顯示線程,可能包含LWP和NLWP列。
[root@centos72 ~]# ps L | head
%cpu %CPU
%mem %MEM
_left LLLLLLLL
_left2 L2L2L2L2
_right RRRRRRRR
_right2 R2R2R2R2
_unlimited U
_unlimited2 U2
alarm ALARM
args COMMAND
[root@centos72 ~]# ps L | tail user USER userns USERNS util C utsns UTSNS uunit UUNIT vsize VSZ vsz VSZ wchan WCHAN wname WCHAN zone ZONE
?
?
?
?
?
?
6和7版本的屬性數量不一樣
[root@centos65 ~]# ps L | wc154 308 3388
?
?
[root@centos72 ~]# ps L | wc169 338 3718
?
?
?
?
?
?
?
?
?
?
?
(二)UNIX風格的選項
?
?
-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
?
?
?
?
?
(1)-e: 顯示所有進程,相當于-A
?
此選項常用
[root@centos72 ~]# ps -e | headPID TTY TIME CMD1 ? 00:00:01 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/0:0H 7 ? 00:00:00 migration/0 8 ? 00:00:00 rcu_bh 9 ? 00:00:00 rcu_sched 10 ? 00:00:00 lru-add-drain 11 ? 00:00:00 watchdog/0 [root@centos72 ~]# ps -e | tail 967 ? 00:00:00 master 968 ? 00:00:00 pickup 969 ? 00:00:00 qmgr 1142 pts/0 00:00:00 bash 1179 ? 00:00:00 kworker/0:0 1191 ? 00:00:00 kworker/0:1 1206 ? 00:00:00 kworker/0:2 1207 ? 00:00:00 kworker/0:3 1228 pts/0 00:00:00 ps 1229 pts/0 00:00:00 tail
?
?
?
?
?
?
(2)-f: 顯示完整格式程序信息
顯示的內容更多
[root@centos72 ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 1928 1924 0 Jul09 pts/0 00:00:01 -bash root 76668 1928 0 12:54 pts/0 00:00:00 ps -f
?
?
?
?
?
?
(3)-L 顯示線程
第4列是線程編號
[root@centos72 ~]# ps -LPID LWP TTY TIME CMD1142 1142 pts/0 00:00:00 bash 1240 1240 pts/0 00:00:00 ps
?
?
?
?
?
[root@centos72 ~]# ps -Lef | grep sshd
root 877 1 877 0 1 16:56 ? 00:00:00 /usr/sbin/sshd -D root 906 877 906 0 1 16:56 ? 00:00:00 sshd: root@pts/0 root 1238 1142 1238 0 1 17:21 pts/0 00:00:00 grep --color=auto sshd
?
?
?
?
?
?
?
?
(4)顯示指定用戶的進程信息
?
-u userlist 指定有效的用戶ID或名稱
[root@centos72 ~]# ps -u root | headPID TTY TIME CMD1 ? 00:00:39 systemd 2 ? 00:00:00 kthreadd 3 ? 00:00:01 ksoftirqd/0 5 ? 00:00:00 kworker/0:0H 7 ? 00:00:00 migration/0 8 ? 00:00:00 rcu_bh 9 ? 00:00:09 rcu_sched 10 ? 00:00:00 lru-add-drain 11 ? 00:00:02 watchdog/0 [root@centos72 ~]# ps -u root | tail 1928 pts/0 00:00:01 bash 3264 ? 00:00:02 kworker/u256:2 86289 ? 00:00:00 kworker/0:0 86830 ? 00:00:00 kworker/u256:0 86866 ? 00:00:00 kworker/0:1 87108 ? 00:00:00 kworker/0:2 87219 pts/1 00:00:00 ps 87220 pts/1 00:00:00 bash 103114 ? 00:00:00 sshd 103122 pts/1 00:00:00 bash
?
?
?
?
?
?
(5)顯示進程的特定屬性
?
?默認按PID顯示的
[root@centos72 ~]# ps -o pid,cmd,%cpu | headPID CMD %CPU1142 -bash 0.0 1248 ps -o pid,cmd,%cpu 0.0 1249 head 0.0
?
?
?
?
?
?
?
?
(6)-C cmdlist 指定命令,多個命令用,分隔
?
[root@centos65 ~]# ps -C vimPID TTY TIME CMD3266 pts/1 00:00:00 vim
?
?
?
?
注意shebang機制一定要寫,否則看不到腳本在運行
查看每個終端的進程
[root@centos65 ~]# ll f1.sh
-rwxr-xr-x. 1 root root 23 May 9 15:32 f1.sh [root@centos65 ~]# cat f1.sh #!/bin/bash sleep 60 [root@centos65 ~]# bash f1.sh
?
?
?
?
[root@centos65 ~]# ps -C f1.shPID TTY TIME CMD
[root@centos65 ~]# ps -C bashPID TTY TIME CMD2018 pts/0 00:00:00 bash 2455 pts/1 00:00:00 bash 3521 pts/1 00:00:00 bash
?
?
?
?
?
兩個bash,一個是登錄bash,一個是執行腳本的bash
[root@centos65 ~]# w15:40:11 up 1:27, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.137.1 14:14 0.00s 0.30s 0.00s w root pts/1 192.168.137.1 14:24 44.00s 0.13s 0.00s bash f1.sh
?
?
?
?
sshd(1632)─┬─sshd(2014)───bash(2018)───pstree(3566)│ └─sshd(2451)───bash(2455)───bash(3554)───sleep(3555)
?
?
?
?
注意不同風格的選項可以混用,但是不是全部的,這樣就可以關注特定的屬性
[root@centos65 ~]# ps -C vim,init o pid,cmd,%memPID CMD %MEM1 /sbin/init 0.13650 vim /etc/fstab 0.4
?
?
?
?
?
(7)-F: 顯示更完整格式的進程信息
?
PSR表示進程運行在那顆CPU上
[root@centos72 ~]# ps -F
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1142 906 0 28992 2692 0 16:56 pts/0 00:00:00 -bash root 1250 1142 0 38831 1844 0 17:42 pts/0 00:00:00 ps -F
?
?
?
?
?
?目前只有1顆CPU
?
?
?
?
?
?
?
如果有多顆CPU就會進行切換,因為進程是有時間片的
時間到了就會釋放CPU的內存空間
[root@centos65 ~]# ps -C vim,init o pid,cmd,%mem,psrPID CMD %MEM PSR1 /sbin/init 0.1 0 3650 vim /etc/fstab 0.4 0
?
?
?
?
?
開啟2個CPU,另外開啟4個終端
并且執行相同的命令
[root@centos65 ~]# dd if=/dev/zero of=/dev/null
?
?
?
?
?
?
?
?
?
[root@centos65 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 2 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 142 Model name: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz Stepping: 9 CPU MHz: 2903.999 BogoMIPS: 5807.99 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 4096K NUMA node0 CPU(s): 0,1
?
?
?
?
?
?
[root@centos65 ~]# ps -C vim,dd o pid,cmd,%mem,tty,psrPID CMD %MEM TT PSR3853 dd if=/dev/zero of=/dev/nul 0.0 pts/1 1 3881 dd if=/dev/zero of=/dev/nul 0.0 pts/2 1 4059 dd if=/dev/zero of=/dev/nul 0.0 pts/3 0 4118 dd if=/dev/zero of=/dev/nul 0.0 pts/4 0
?
?
?
?
使用watch查看
[root@centos65 ~]# watch -n0.1 ps -C vim,dd o pid,cmd,%mem,tty,psr
?
?
?
?
在6上沒有看到變化,在7上看看
開啟2個CPU,3個終端
?
?
?
?
?
?
?
[root@centos72 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 2 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 142 Model name: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz Stepping: 9 CPU MHz: 2903.999 BogoMIPS: 5807.99 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 4096K NUMA node0 CPU(s): 0,1 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt arat
?
?
?
?
?
?
?可以很明顯的看到變化,說明和版本有關系
7這樣可以充分利用CPU資源。避免了一核有難七核圍觀的尷尬情況
[root@centos72 ~]# ps -C vim,dd o pid,cmd,%mem,tty,psrPID CMD %MEM TT PSR8323 dd if=/dev/zero of=/dev/nul 0.0 pts/1 0 8369 dd if=/dev/zero of=/dev/nul 0.0 pts/2 0 8485 dd if=/dev/zero of=/dev/nul 0.0 pts/3 1 [root@centos72 ~]# ps -C vim,dd o pid,cmd,%mem,tty,psr PID CMD %MEM TT PSR 8323 dd if=/dev/zero of=/dev/nul 0.0 pts/1 0 8369 dd if=/dev/zero of=/dev/nul 0.0 pts/2 1 8485 dd if=/dev/zero of=/dev/nul 0.0 pts/3 1
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
普通用戶wang執行passwd有suid權限,生效的是root
而發起這個進程的用戶是wang
[root@centos72 ~]# su - wang
[wang@centos72 ~]$ ls
[wang@centos72 ~]$ pwd
/home/wang [wang@centos72 ~]$ passwd Changing password for user wang. Changing password for wang. (current) UNIX password:
?
?
?
?
[root@centos72 ~]# ps -u wangPID TTY TIME CMD9274 pts/1 00:00:00 bash [root@centos72 ~]# ps -U wang PID TTY TIME CMD 9274 pts/1 00:00:00 bash 9390 pts/1 00:00:00 passwd
?
?
?
?
當普通用戶運行具有suid權限的文件時候,身份就變成了這個文件的所有者
[root@centos72 ~]# which passwd
/usr/bin/passwd
[root@centos72 ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd [root@centos72 ~]# file /usr/bin/passwd /usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1e5735bf7b317e60bcb907f1989951f6abd50e8d, stripped
?
?
?
?
?
?
?
(8)-p pid 顯示指pid對應的進程
[root@centos72 ~]# ps -p 1PID TTY TIME CMD1 ? 00:00:02 systemd
?
?
?
[root@centos65 ~]# ps -p 1PID TTY TIME CMD1 ? 00:00:01 init
?
?
?
?
部分顯示進程信息
[root@centos65 ~]# ps -p 1 o pid,%mem,cmdPID %MEM CMD1 0.1 /sbin/init
?
?
?
?
?
?
普通用戶執行了passwd
[root@centos65 ~]# su - wang
[wang@centos65 ~]$ ls
1.sh [wang@centos65 ~]$ vim /etc/fstab [wang@centos65 ~]$ passwd Changing password for user wang. Changing password for wang. (current) UNIX password:
?
?
?
?
[root@centos65 ~]# ps -C passwd o pid,%mem,cmdPID %MEM CMD5229 0.1 passwd
?
?
?
?
[root@centos72 ~]# su - wang
[wang@centos72 ~]$ ls
[wang@centos72 ~]$ pwd
/home/wang [wang@centos72 ~]$ passwd Changing password for user wang. Changing password for wang. (current) UNIX password:
?
?
?
[root@centos72 ~]# ps -C passwd o pid,%mem,cmdPID %MEM CMD9390 0.1 passwd
?
?
?
?
?
?
?
常用選項組合ps -ef
?
?
C表示CPU的利用率
[root@centos65 ~]# ps -ef | head
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 14:12 ? 00:00:01 /sbin/init root 2 0 0 14:12 ? 00:00:00 [kthreadd] root 3 2 0 14:12 ? 00:00:00 [migration/0] root 4 2 0 14:12 ? 00:00:00 [ksoftirqd/0] root 5 2 0 14:12 ? 00:00:00 [stopper/0] root 6 2 0 14:12 ? 00:00:00 [watchdog/0] root 7 2 0 14:12 ? 00:00:01 [events/0] root 8 2 0 14:12 ? 00:00:00 [events/0] root 9 2 0 14:12 ? 00:00:00 [events_long/0]
?
?
?
?
?
顯示所有進程
[root@centos72 ~]# ps -ax | headPID TTY STAT TIME COMMAND1 ? Ss 0:39 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 2 ? S 0:00 [kthreadd] 3 ? S 0:01 [ksoftirqd/0] 5 ? S< 0:00 [kworker/0:0H] 7 ? S 0:00 [migration/0] 8 ? S 0:00 [rcu_bh] 9 ? R 0:09 [rcu_sched] 10 ? S< 0:00 [lru-add-drain] 11 ? S 0:02 [watchdog/0] [root@centos72 ~]# ps -ax | tail 3264 ? S 0:02 [kworker/u256:2] 85397 ? S 0:00 pickup -l -t unix -u 86289 ? S 0:00 [kworker/0:0] 86830 ? S 0:00 [kworker/u256:0] 86866 ? S 0:00 [kworker/0:1] 87108 ? S 0:00 [kworker/0:2] 87347 pts/1 R+ 0:00 ps -ax 87348 pts/1 R+ 0:00 tail 103114 ? Ss 0:00 sshd: root@pts/1 103122 pts/1 Ss 0:00 -bash
?
?
?
?
[root@centos65 ~]# ps -axo pid,cmd,%cpu | head
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ PID CMD %CPU 1 /sbin/init 0.0 2 [kthreadd] 0.0 3 [migration/0] 0.0 4 [ksoftirqd/0] 0.0 5 [stopper/0] 0.0 6 [watchdog/0] 0.0 7 [events/0] 0.0 8 [events/0] 0.0 9 [events_long/0] 0.0
?
?
?
?
?
?
?
?
?
?
(三)GNU風格的選項
?
?
?
按照命令來排序
[root@centos65 ~]# ps -axo pid,cmd,%cpu --sort cmd | head
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ PID CMD %CPU 1761 abrt-dump-oops -d /var/spoo 0.0 1751 /usr/sbin/abrtd 0.0 1379 /usr/sbin/acpid 0.0 37 [aio/0] 0.0 14 [async/mgr] 0.0 23 [ata_aux] 0.0 24 [ata_sff/0] 0.0 1808 /usr/sbin/atd 0.0 1203 auditd 0.0
?
?
?
?
?
?
--ppid pid 顯示屬于pid的子進程
注意不會顯示線程
[root@centos72 ~]# pstree -p
systemd(1)─┬─NetworkManager(539)─┬─{NetworkManager}(570)│ └─{NetworkManager}(572) ├─VGAuthService(526) ├─agetty(552) ├─auditd(501)───{auditd}(502) ├─crond(545) ├─dbus-daemon(529)───{dbus-daemon}(538) ├─httpd(2210)─┬─httpd(2212) │ ├─httpd(2213) │ ├─httpd(2214) │ ├─httpd(2215) │ └─httpd(2216) ├─master(849)─┬─pickup(6815) │ └─qmgr(851) ├─polkitd(541)─┬─{polkitd}(564) │ ├─{polkitd}(565) │ ├─{polkitd}(566) │ ├─{polkitd}(567) │ └─{polkitd}(568) ├─rsyslogd(766)─┬─{rsyslogd}(769) │ └─{rsyslogd}(770) ├─sshd(763)─┬─sshd(1507)───bash(1511) │ ├─sshd(1531)───bash(1535)───pstree(10043) │ ├─sshd(8337)───bash(8345)───dd(8369) │ └─sshd(8457)───bash(8465)───dd(8485) ├─systemd-journal(347) ├─systemd-logind(528) ├─systemd-udevd(372) ├─tuned(765)─┬─{tuned}(1006) │ ├─{tuned}(1007) │ ├─{tuned}(1008) │ └─{tuned}(1022) └─vmtoolsd(527)───{vmtoolsd}(553)
?
?
?
?
?
[root@centos72 ~]# ps --ppid 2210PID TTY TIME CMD2212 ? 00:00:00 httpd 2213 ? 00:00:00 httpd 2214 ? 00:00:00 httpd 2215 ? 00:00:00 httpd 2216 ? 00:00:00 httpd
?
?
?
?
?
[root@centos65 ~]# pstree
init─┬─abrt-dump-oops├─abrtd├─acpid├─atd├─auditd───{auditd}├─automount───4*[{automount}]├─crond├─dbus-daemon───{dbus-daemon}├─dnsmasq├─hald─┬─hald-runner─┬─hald-addon-acpi │ │ └─hald-addon-inpu │ └─{hald} ├─httpd.worker───3*[httpd.worker───26*[{httpd.worker}]] ├─ksmtuned───sleep ├─libvirtd───10*[{libvirtd}] ├─master─┬─pickup │ └─qmgr ├─6*[mingetty] ├─rpc.idmapd ├─rpc.mountd ├─rpc.rquotad ├─rpc.statd ├─rpcbind ├─rsyslogd───3*[{rsyslogd}] ├─sshd─┬─sshd───bash │ └─sshd───bash───pstree ├─udevd───2*[udevd] └─xinetd
?
?
?
?
[root@centos65 ~]# ps --ppid 2298PID TTY TIME CMD2300 ? 00:00:00 httpd.worker 2301 ? 00:00:00 httpd.worker 2303 ? 00:00:00 httpd.worker
?