(5)ps詳解 (每週一個linux命令系列)

(5)ps詳解 (每週一個linux命令系列)

linux命令 ps詳解


引言:今天的命令是用來看進程狀態的ps命令

ps

我們先看man ps


 ps - report a snapshot of the current processes.

翻譯:顯示當前進程的快照。ps是 Process Status的縮寫

具體的描述如下:


DESCRIPTION
       ps displays information about a selection of the active processes.  If you
       want a repetitive update of the selection and the displayed information, use
       top(1) instead.
       ps展示了當前運行的程序的一些選項信息快照,如果想要查看實時更新的程序信息,請使用top命令。

       This version of ps accepts several kinds of options:
       ps可以接收多種不同的選項
      
       
       1   UNIX options, which may be grouped and must be preceded by a dash.
       2   BSD options, which may be grouped and must not be used with a dash.
       2   GNU long options, which are preceded by two dashes.
       1   UNIX選項,需要分組使用,並且必須以破折號開頭。
       2   BSD選項,需要分組使用,不得與破折號一起使用。
       3   GNU長選項,前面有兩個破折號。

       Options of different types may be freely mixed, but conflicts can appear.
       There are some synonymous options, which are functionally identical, due to
       the many standards and ps implementations that this ps is compatible with.
        不同類型的選項可以自由混合,但可能出現衝突。
        由於ps兼容的許多標準和ps實現,有一些在功能上相同的同義選項。


       Note that "ps -aux" is distinct from "ps aux".  The POSIX and UNIX standards
       require that "ps -aux" print all processes owned by a user named "x", as well
       as printing all processes that would be selected by the -a option.  If the
       user named "x" does not exist, this ps may interpret the command as "ps aux"
       instead and print a warning.  This behavior is intended to aid in
       transitioning old scripts and habits.  It is fragile, subject to change, and
       thus should not be relied upon.
       請注意,“ps -aux”與“ps aux”不同。 
       POSIX和UNIX標準要求“ps -aux”打印名爲“x”的用戶擁有的所有進程,
       以及打印將由-a選項選擇的所有進程。如果名爲“x”的用戶不存在,則此ps可能會將
       該命令解釋爲“ps aux”且打印警告。此行爲旨在幫助轉換舊腳本和習慣。
       這不一定是可靠的,儘量不要這樣用

       By default, ps selects all processes with the same effective user ID
       (euid=EUID) as the current user and associated with the same terminal as the
       invoker.  It displays the process ID (pid=PID), the terminal associated with
       the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format
       (time=TIME), and the executable name (ucmd=CMD).  Output is unsorted by
       default.
       默認情況下,ps選擇與當前用戶具有相同有效用戶ID(euid = EUID)且與調用者相同的終端關聯的所有進程。
       它顯示進程ID(pid = PID),與進程關聯的終端(tname = TTY),
       累計CPU時間[DD-] hh:mm:ss格式(time = TIME)和可執行文件名(ucmd) = CMD)。
       默認情況下,不對輸出排序。

       The use of BSD-style options will add process state (stat=STAT) to the
       default display and show the command args (args=COMMAND) instead of the
       executable name.  You can override this with the PS_FORMAT environment
       variable. The use of BSD-style options will also change the process selection
       to include processes on other terminals (TTYs) that are owned by you;
       alternately, this may be described as setting the selection to be the set of all processes filtered to exclude processes owned by other users or not on a terminal.  These effects are not considered when options are described as being "identical" below, so -M will be considered identical to Z and so on.
       使用BSD樣式選項會將進程狀態(stat = STAT)添加到默認顯示,並顯示命令args(args = COMMAND)
       而不是可執行文件名。您可以使用PS_FORMAT環境變量覆蓋它。使用BSD風格的選項也會改變進程的選擇,
       以包括您擁有的其他終端(TTY)上的進程;或者,這可以被描述爲將選擇過濾設置爲排除其他用戶所擁有的和
       不在終端上的進程的集合。當選項在下面被描述爲“相同”時,不需要考慮這些效果,因此-M將被視爲與Z相同
       依此類推。
       
       Except as described below, process selection options are additive.  The
       default selection is discarded, and then the selected processes are added to the set of processes to be displayed.  A process will thus be shown if it meets any of the given selection criteria.
       除以下描述外,進程選擇選項是附加的。將丟棄默認選擇,然後將所選進程添加到要顯示的進程集。因此,如果滿足任何給定的選擇標準,將顯示該進程。
       

我們再來看一下幫助文檔

dev1:~$ ps --help all

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

Selection by list:
 -C <command>         command name
 -G, --Group <GID>    real group id or name
 -g, --group <group>  session or effective group name
 -p, p, --pid <PID>   process id
        --ppid <PID>  parent process id
 -q, q, --quick-pid <PID>
                      process id (quick mode)
 -s, --sid <session>  session id
 -t, t, --tty <tty>   terminal
 -u, U, --user <UID>  effective user id or name
 -U, --User <UID>     real user id or name

  The selection options take as their argument either:
    a comma-separated list e.g. '-u root,nobody' or
    a blank-separated list e.g. '-p 123 4567'

Output formats:
 -F                   extra full
 -f                   full-format, including command lines
  f, --forest         ascii art process tree
 -H                   show process hierarchy
 -j                   jobs format
  j                   BSD job control format
 -l                   long format
  l                   BSD long format
 -M, Z                add security data (for SELinux)
 -O <format>          preloaded with default columns
  O <format>          as -O, with BSD personality
 -o, o, --format <format>
                      user-defined format
  s                   signal format
  u                   user-oriented format
  v                   virtual memory format
  X                   register format
 -y                   do not show flags, show rss vs. addr (used with -l)
     --context        display security context (for SELinux)
     --headers        repeat header lines, one per page
     --no-headers     do not print header at all
     --cols, --columns, --width <num>
                      set screen width
     --rows, --lines <num>
                      set screen height

Show threads:
  H                   as if they were processes
 -L                   possibly with LWP and NLWP columns
 -m, m                after processes
 -T                   possibly with SPID column

Miscellaneous options:
 -c                   show scheduling class with -l option
  c                   show true command name
  e                   show the environment after command
  k,    --sort        specify sort order as: [+|-]key[,[+|-]key[,...]]
  L                   show format specifiers
  n                   display numeric uid and wchan
  S,    --cumulative  include some dead child process data
 -y                   do not show flags, show rss (only with -l)
 -V, V, --version     display version information and exit
 -w, w                unlimited output width

        --help <simple|list|output|threads|misc|all>
                      display help and exit

打印所有用戶的運行進程

a 顯示所有用戶的進程
u 顯示進程的所有者
x 顯示非終端啓用的進程
注意之前介紹了 aux和-au的區別

輸出:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     28726  0.0  0.0      0     0 ?        S    10:42   0:00 [kworker/3:0]
root     28889  0.0  0.0      0     0 ?        S    10:44   0:00 [kworker/0:2]
root     28979  0.0  0.0      0     0 ?        S    10:45   0:00 [kworker/2:2]
root     29014  0.0  0.0      0     0 ?        S    10:45   0:00 [kworker/1:0]

列屬性說明 其他的列參考man ps吧

 - USER:該進程屬於那個使用者賬號的
 - PID :該進程的號碼
 - %CPU:該進程所使用的CPU時間百分比。  
 - %MEM:該進程所使用的CPU時間百分比
 - VSZ :該進程佔用的虛擬內存量 (Kbytes)
 - RSS :該進程佔用的固定的內存量 (Kbytes)
 - TTY :該進程是在那個終端機上面運作,若與終端機無關,則顯示 ?,
         另外, tty1-tty6 是本機上面的登入者程序,若爲 pts/0等等的,則表示爲由網絡連接進主機的程序。
 - STAT:Process Status  該程序目前的狀態,主要的狀態有
            D :不可中斷的睡眠. 通常是處於I/O之中
            R :該程序目前正處於運行隊列中.運行中/可運行.
            S :該程序目前正在睡眠當中,但可被某些事件或訊號 (signal) 觸發喚醒。
            T :已停止. 可能是因爲shell作業控制或進程正被調試.
            Z :該程序應該已經終止,但是其父程序沒有終止他,造成 zombie (疆屍) 程序的狀態
            注:進程狀態還有其他的幾種但是一般不常用
 - START    :該 process 被觸發啓動的時間
 - TIME     :該 process 實際使用 CPU 運作的時間
 - COMMAND  :該程序的實際指令

接下來說一些常用命令

最常用的就是和grep配合 查找特定的進程了

ps -ef | grep ssh

輸出

root      1026     1  0 10月02 ?      00:00:08 /usr/sbin/sshd -D
root      1534  1026  0 10月08 ?      00:00:00 sshd: xxx [priv]
xxx  1536  1534  0 10月08 ?      00:00:01 sshd: xxx@pts/1,pts/2,pts/3

定製輸出的列屬性

我們可以使用-o來定製列屬性
ps -o uid,%cpu,%mem,pid,ppid,pgrp,session,tpgid,comm

查看特定用戶的進程

在需要查看特定用戶進程的情況下,我們可以使用 -u 參數。比如查看vinter用戶的進程

ps -u vinter

對輸出結果進行排序

升序和降序是通過在字段前面增加負號來設置的 有負號‘-’代表降序
ps --sort ppid 升序
ps --sort -ppid 降序

查看某個名稱的進程

例如查看名爲ps的進程

ps -C ps

查看某個PID的進程

ps -P 23178

把ps的結果輸出到文件或分頁查看

可以用 | 管道和 more 連接起來分頁查看。
ps aux |more
把ps結果輸出到ps.txt文本。
ps aux > ./ps.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章