ps命令
用於列出執行ps命令的那個時刻的進程快照,就像用手機給進程照了一張照片。如果想要動態地顯示進程的信息,就需要使用top命令,該命令類似於把手機切換成錄像模式。
因為ps命令的功能實在是太多了,26個字母已經滿足不了,因此在ps命令的參數中有類似於-a與a這2種寫法,這2種寫法的功能是不一樣的。
參數的格式具體如下。
UNIX格式:一個“-”開頭。
BSD格式:沒有“-”開頭。
GNU長格式:兩個“-”開頭。
ps命令的參數選項及說明
-a 顯示所有終端下執行的進程 a 顯示與終端相關的所有進程,包含每個進程的完整路徑 x 顯示與終端無關的所有進程 u 顯示進程的用戶信息 -u 顯示指定用戶相關的進程信息 -e 顯示所有進程 -f 額外顯示UID、PPID、C與STIME欄位 f 顯示進程樹 -H 顯示進程樹 -l 以詳細的格式來顯示進程的狀況 -o 自定義輸出指定的字段,以退號分隔 -sot key key表示為指定字段排序,默認升序,+key升序,key降序
ps命令不接任何參數
[root@doit ~]# ps PID TTY TIME CMD 9940 pts/0 00:00:00 bash 25146 pts/0 00:00:00 ps 其輸出結果中的各項說明如下。 PID是進程的標識號。 TTY是進程所屬的終端控制台。 TIME列是進程所使用的總的CPU時間。 CMD列是正在執行的命令行。
ps命令常用操作組合(命令1)
[root@doit ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Apr22 ? 00:03:35 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0 0 Apr22 ? 00:00:00 [kthreadd] root 3 2 0 Apr22 ? 00:00:00 [rcu_gp] root 4 2 0 Apr22 ? 00:00:00 [rcu_par_gp] root 6 2 0 Apr22 ? 00:00:00 [kworker/0:0H-kb] root 8 2 0 Apr22 ? 00:00:00 [mm_percpu_wq] root 9 2 0 Apr22 ? 00:00:00 [ksoftirqd/0] root 10 2 0 Apr22 ? 00:01:28 [rcu_sched] root 11 2 0 Apr22 ? 00:00:03 [migration/0] root 13 2 0 Apr22 ? 00:00:00 [cpuhp/0] root 14 2 0 Apr22 ? 00:00:00 [cpuhp/1] root 15 2 0 Apr22 ? 00:00:02 [migration/1] root 16 2 0 Apr22 ? 00:00:05 [ksoftirqd/1] 輸出信息中各列的說明如下。 UID:進程被該UID所擁有。 PID:進程的標識號。 PPID:進程的父進程的標識號。 C:CPU使用的資源百分比。 STIME:進程開始的時間。 TTY:該進程是在哪個終端機上面運作,若與終端機無關,則顯示“?”,另外,ttyl-tty6是本機上面的登入者進程,若為pts/0等,則表示為由網絡連接進主機的進程。 TIME:進程所使用的總的CPU時間。 CMD:正在執行的命令行。 下面是ps與grep的組合用法,用於查找特定進程,比如查找sshd進程等。 [root@doit ~]# ps -ef|grep ssh root 3503 1 0 Apr22 ? 00:00:00 /usr/sbin/sshd -D root 9935 3503 0 17:04 ? 00:00:00 sshd: root@pts/0 root 25345 9940 0 22:18 pts/0 00:00:00 grep --color=auto ssh
ps命令常用操作組合(命令2)
這里使用BSD語法格式顯示每個進程信息。 [root@doit ~]# ps aux # BSD格式參數,使用a選項和x選項顯示所有進程,使用u選項顯示進程的用戶信息。 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 191024 5320 ? Ss Apr22 3:35 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0.0 0.0 0 0 ? S Apr22 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< Apr22 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< Apr22 0:00 [rcu_par_gp] root 6 0.0 0.0 0 0 ? I< Apr22 0:00 [kworker/0:0H-kb] root 8 0.0 0.0 0 0 ? I< Apr22 0:00 [mm_percpu_wq] root 9 0.0 0.0 0 0 ? S Apr22 0:00 [ksoftirqd/0] root 10 0.0 0.0 0 0 ? I Apr22 1:28 [rcu_sched] root 11 0.0 0.0 0 0 ? S Apr22 0:03 [migration/0] root 13 0.0 0.0 0 0 ? S Apr22 0:00 [cpuhp/0] root 14 0.0 0.0 0 0 ? S Apr22 0:00 [cpuhp/1] root 15 0.0 0.0 0 0 ? S Apr22 0:02 [migration/1] root 16 0.0 0.0 0 0 ? S Apr22 0:05 [ksoftirqd/1] root 18 0.0 0.0 0 0 ? I< Apr22 0:00 [kworker/1:0H-kb] root 19 0.0 0.0 0 0 ? S Apr22 0:00 [kdevtmpfs] root 20 0.0 0.0 0 0 ? I< Apr22 0:00 [netns] root 21 0.0 0.0 0 0 ? S Apr22 0:00 [kauditd] root 22 0.0 0.0 0 0 ? S Apr22 0:00 [khungtaskd] root 23 0.0 0.0 0 0 ? S Apr22 0:00 [oom_reaper] 輸出信息中各列的說明如下。 USER:該進程屬於的用戶。 PID:該進程的進程號。 %CPU:該進程使用掉的CPU資源百分比。 %MEM:該進程所占用的物理內存百分比。 VSZ:該進程使用掉的虛擬內存量(單位為Kbytes)。 RSS:該進程占用的固定的內存量(單位為Kbytes)。 TTY:該進程是在哪個終端機上面運作的,若與終端機無關,則顯示“?”,另外,ttyl-tty6是本機上面的登入者進程,若為pts/0等,則表示為由網絡連接進主機的 進程。 STAT:該進程目前的狀態,主要的狀態包括如下幾種。 R:正在運行,或者是可以運行。 S:正在中斷睡眠中,可以由某些信號(signal)喚醒。 D:不可中斷睡眠。 T:正在偵測或者是停止了。 Z:已經終止,但是其父進程無法正常終止它,從而變成zombie(僵屍)進程的狀態。 +:前台進程。 l:多線程進程。 N:低優先級進程。 <:高優先級進程。 s:進程領導者。 L:已將頁面鎖定到內存中。 START:該進程被觸發啟動的時間。 TIME:該進程實際使用CPU運作的時間。 COMMAND:該進程的實際命令。
顯示指定用戶的相關進程信息。
[root@cs6 ~]# ps -u root PID TTY TIME CMD 1 ? 00:00:01 init 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 [root@cs6 ~]# ps -l #<==UNIX格式參數,使用參數-l以詳細的格式顯示進程的狀況。 F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1797 1793 0 80 0 - 27099 do_wai pts/1 00:00:00 bash 4 R 0 1934 1797 2 80 0 - 27034 - pts/1 00:00:00 ps 輸出信息中各列的說明如下。 F:代表這個進程的標志(flag),4代表使用者為super user。 S:代表這個進程的狀態(STAT),前面已經講解過。 UID:進程被該UID所擁有。 PID:進程的標識號。 PPID:父進程的ID。 C:CPU使用的資源百分比。 PRI:Priority(優先執行序)的縮寫。 NI:Nice值。 ADDR:指出該進程在內存的哪個部分。如果是個running的進程,則一般是“-”。 SZ:使用掉的內存大小。 WCHAN:目前這個進程是否正在運作當中,若為“-”則表示正在運作。 TTY:該進程是在哪個終端機上面運作的,若與終端機無關,則顯示“?”,另外,ttyl-tty6是本機上面的登入者進程,若為pts/0等,則表示為由網絡連接進主機的進程。 TIME:該進程實際使用CPU運作的時間。 CMD:該進程的實際命令。
顯示進程樹
[root@cs6 ~]# ps -eH 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 [root@cs6 ~]# ps axf PID TTY STAT TIME COMMAND 2 ? S 0:00 [kthreadd] 3 ? S 0:00 _ [migration/0] 4 ? S 0:00 _ [ksoftirqd/0] 519 ? S<s 0:00 /sbin/udevd -d 1481 ? S< 0:00 _ /sbin/udevd -d 1482 ? S< 0:00 _ /sbin/udevd -d 1268 ? S<sl 0:00 auditd 1291 ? Sl 0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5 1357 ? Ss 0:00 /usr/sbin/sshd 1793 ? Ss 0:00 _ sshd: root@pts/1 1797 pts/1 Ss 0:00 | _ -bash 1938 pts/1 R+ 0:00 | _ ps axf 1826 ? Ss 0:00 _ sshd: root@pts/0 1830 pts/0 Ss+ 0:00 _ -bash 1436 ? Ss 0:00 /usr/libexec/postfix/master 1455 ? S 0:00 _ qmgr -l -t fifo -u 1880 ? S 0:00 _ pickup -l -t fifo -u 1448 ? Ss 0:01 crond
輸出指定的字段。
(1)AIX格式:ps -eo "%p %y %x %c”
語法說明:-e是顯示所有進程,-o表示可以自定義輸出指定的字段,以逗號分隔,其支持的字段見下面的代碼。
(2)標准格式:ps-eo pid,user,args--sort pid
其支持的字段比較多,請大家查詢man ps並搜索 STANDARD FORMAT SPECIFIERS來了解。
[root@cs6 ~]# ps -o pid,ppid,pgrp,session,tpgid,comm PID PPID PGRP SESS TPGID COMMAND 1797 1793 1797 1797 1949 bash 1949 1797 1949 1797 1949 ps
查看進程並按vsz列從大到小排列,--sort vsz或--sort +vs2表示從小到大升序排列,--Sort
-vsz表示從大到小降序排列。
[root@cs6 ~]# ps -eo "%C : %p : %z : %a" --sort -vsz %CPU : PID : VSZ : COMMAND 0.0 : 1291 : 255424 : /sbin/rsyslogd -i /var/run/syslogd.pid -c 5 0.0 : 1448 : 116880 : crond 0.0 : 1797 : 108396 : -bash 0.0 : 1830 : 108316 : -bash 0.0 : 1951 : 108240 : ps -eo %C : %p : %z : %a --sort -vsz 0.0 : 1793 : 102132 : sshd: root@pts/1 0.0 : 1826 : 102132 : sshd: root@pts/0 0.0 : 1455 : 81260 : qmgr -l -t fifo -u 0.0 : 1880 : 81092 : pickup -l -t fifo -u 0.0 : 1436 : 81012 : /usr/libexec/postfix/master 0.0 : 1357 : 66288 : /usr/sbin/sshd 0.0 : 1268 : 29764 : auditd 0.0 : 1 : 19232 : /sbin/init 0.0 : 1481 : 12376 : /sbin/udevd -d 0.0 : 1482 : 12376 : /sbin/udevd -d 0.0 : 519 : 11300 : /sbin/udevd -d 0.0 : 1463 : 4060 : /sbin/mingetty /dev/tty1 0.0 : 1465 : 4060 : /sbin/mingetty /dev/tty2 0.0 : 1467 : 4060 : /sbin/mingetty /dev/tty3 0.0 : 1469 : 4060 : /sbin/mingetty /dev/tty4 0.0 : 1471 : 4060 : /sbin/mingetty /dev/tty5 0.0 : 1473 : 4060 : /sbin/mingetty /dev/tty6
查看某個進程在哪個CPU上運行:
[root@cs6 ~]# ps -eo pid,args,psr PID COMMAND PSR 1 /sbin/init 0 2 [kthreadd] 0 3 [migration/0] 0 4 [ksoftirqd/0] 0 5 [stopper/0] 0 6 [watchdog/0] 0 7 [migration/1] 1 8 [stopper/1] 1 9 [ksoftirqd/1] 1 10 [watchdog/1] 1 11 [events/0] 0 12 [events/1] 1 13 [events/0] 0 14 [events/1] 1 15 [events_long/0] 0 16 [events_long/1] 1