ps更全面的介紹見官網。一個簡單的常見命令,竟也這么復雜民鼓,要學的內容很多。用的時候查查字典。
幾點感觸:1) ps三種風格參數(shù)選項焕阿;2) note中ps的原理,數(shù)據(jù)從/proc中讀取數(shù)據(jù)首启;3) note中僵尸進程暮屡;4) 根據(jù)需要不同場景下的ps命令。
ps - report a snapshot of the current? processes.
ps 僅僅顯示查詢當前活動進程的相關信息毅桃。如果要重復查詢進程動態(tài)變化的相關信息栽惶,請使用TOP命令。
ps命令的3種參數(shù)選項:
1? UNIX options 可以分組疾嗅,且參數(shù)選項前面必須要有一個破折號外厂。
2? BSD options 可以分組,但參數(shù)選項前面不能有一個破折號代承。
3? GNU long options參數(shù)選項前面必須要有兩個破折號汁蝶。
注意:ps -aux和ps aux的區(qū)別。The POSIX and UNIX標準要求ps -aux通過-a參數(shù)來打印出所有進程信息和-x參數(shù)打印其用戶叫x的進程的信息论悴。假如x用戶不存在掖棉,會打印警告信息。其旨在幫助轉換舊腳本和習慣膀估。它很脆弱幔亥,可能會發(fā)生變化,因此不應依賴它察纯。
默認情況下帕棉,PS選擇將相同有效用戶ID(euid =EUID)作為當前用戶并與調用者相關聯(lián)的所有進程针肥。PS展示了進程id(pid=PID),進程相關的終端(tname=TTY)香伴,累積起來的CPU占用時間(time=TIME)慰枕,以及可執(zhí)行文件名(ucmd=CMD)。默認情況下即纲,PS輸出不排序具帮。
BSD風格的參數(shù)選項,默認情況下將增加字段顯示:進程狀態(tài) (stat=STAT)低斋,顯示命令參數(shù)(args=COMMAND)而不是可執(zhí)行文件名蜂厅。You can override this with the PS_FORMAT environment variable.
example
使用標準格式查看系統(tǒng)上的所有進程:ps -e、ps -ef膊畴、 ps -eF葛峻、 ps -ely
使用BSD格式查看系統(tǒng)上的每一個進程:ps ax、ps axu
打印進程樹: ps -ejH巴比、 ps axjf
打印線程相關信息: ps -eLf术奖、ps axms
打印安全(用戶相關)信息:ps -eo euser,ruser,suser,fuser,f,comm,label、 ps axZ轻绞、 ps -eM
打印以root用戶運行的所有進程: ps -U root -u root u
打印用戶自定義格式下的所有進程: ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm采记、 ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm、ps -Ao pid,tt,user,fname,tmout,f,wchan
打印syslogd名字有關的進程PID:ps -C syslogd -o pid=政勃, -C cmdlist
打印PID為42有關的進程名字: ps -q 42 -o comm=
note
This ps works by reading the virtual files in /proc. This ps does not need to be setuid? kmem or have any privileges to run.Do not give this ps any special permissions.
CPU usage is currently expressed as the? percentage of time spent running during? the entire lifetime of a process.? This is not? ideal, and it does not conform to the standards that ps otherwise conforms to.? CPU usage is unlikely to add up to exactly 100%.
The SIZE and RSS fields don't count some parts of a process including the page tables, kernel stack, struct thread_info, and struct task_struct.? This is usually at least 20 KiB of memory that is always resident.? SIZE is the virtual size of the process? (code+data+stack).
Processes marked <defunct> are dead processes (so-called "zombies") that remain? because their parent has not destroyed them properly.? These processes will be destroyed by init(8) if the parent process exits.
If the length of the username is greater? than the length of the display column, the? username will be truncated.? See the -o and? -O formatting options to customize length.
Commands options such as ps -aux are? not recommended as it is a confusion of? two different standards.? According to the? POSIX and UNIX standards, the above? command asks to display all processes? with a TTY (generally the commands users are running) plus all processes owned by? a user named x.? If that user doesn't exist,? then ps will assume you really meant ps aux.