在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中的参数对显示样式进行调整,例如树状显示等。