在linux中,ps指令是一個常用而且很複雜的指令,因爲參數和組合非常的多,甚至加不加-又會出現很多情況。
希望完全通過記憶掌握ps指令是比較困難的,好在linux爲我們提供了很好的提示。
當我們輸入錯誤信息的時候,linux會提供一下信息:
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
-q by process ID (unsorted & quick)
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
--quick-pid
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
稍加理解,我們便可以很容易的對其進行組合。
舉幾個常見的例子我們分析一下使用規則:
ps aux
其中a 屬於simple selection區域,顧名思義是決定選擇範圍的,功能是顯示所有用戶的屬於tty的進程。u屬於output format控制的是輸出的格式,u的作用是以用戶爲主進行顯示。x與a屬於同一個區域,會顯示不屬於任何tty的進程,與a組合可以顯示更多。
ps axjf
ax我們已經做了解釋,j屬於格式區域,用工作模式顯示,f屬於misc options區域,功能是樹狀顯示,這裏要注意f 與 -f屬於不同的區域功能也不同。ps f與ps -f有很大的區別,如果想要使用-f與axj組合 則指令爲 ps axj -f 事實上這邊會與j衝突,經嘗試,output format區域參數一般只能使用一個。
以上指令中,我們已經使用了三種類型的參數
還有一個selection by list區域中的參數,以-U爲例,我們使用ps -U 1,500即可選擇uid爲1或者500的進程。同樣可以與其他指令組合 例如 ps axjf -u 500是有效的.
long optionszhong 中的參數有些是其他區域參數的全稱,也有一些特有的 同樣可以組合使用例如:ps
-aux --sort -pcpu根據CPU使用排序
根據嘗試及分析,可以基本得到以下使用規則:
1、使用simple selection區域的一個或多個參數,決定顯示進程的範圍。(注意有部分參數之間會衝突)
2、使用selection by list區域中的一個或多個參數,對進程進行限制篩選,如果使用了多個。
(重點:以上兩類參數組合使用的時候,使用的是“或”的邏輯。)
3、使用output format參數控制輸出的內容和格式,一般一次只能使用一個。
4、使用long options中的參數進行排序等操作。
5、使用misc options中的參數對顯示樣式進行調整,例如樹狀顯示等。