ps命令详解-psaux与ps-ef区别
Linux中的ps命令是Process Status的缩写。ps命令⽤来列出系统中当前运⾏的那些进程。ps命令列出的是当前那些进程的快照,就是执⾏ps命令的那个时刻的那些进程,如果想要动态的显⽰进程信息,就可以使⽤top命令。
要对进程进⾏监测和控制,⾸先必须要了解当前进程的情况,也就是需要查看当前进程,⽽ ps 命令就是最基本同时也是⾮常强⼤的进程查看命令。使⽤该命令可以确定有哪些进程正在运⾏和运⾏的状态、进程是否结束、进程有没有僵死、哪些进程占⽤了过多的资源等等。总之⼤部分信息都是可以通过执⾏该命令得到的。
ps 为我们提供了进程的⼀次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该⽤ top ⼯具。kill 命令⽤于杀死进程。
linux上进程有5种状态:grep命令查看进程
1. 运⾏(正在运⾏或在运⾏队列中等待)
2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)
3. 不可中断(收到信号不唤醒和不可运⾏, 进程必须等待直到有中断发⽣)
4. 僵死(进程已终⽌, 但进程描述符存在, 直到⽗进程调⽤wait4()系统调⽤后释放)
5. 停⽌(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停⽌运⾏运⾏)
ps⼯具标识进程的5种状态码:
D 不可中断 uninterruptible sleep (usually IO)
R 运⾏ runnable (on run queue)
S 中断 sleeping
T 停⽌ traced or stopped
Z 僵死 a defunct (”zombie”) process
这⾥重点讨论的是ps aux和ps –aux的区别,及ps aux和ps –ef的区别。
1. ps aux和ps –aux
man ps 之后得到的结果:
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.
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",⽽且会打印⼀个警告。这种⾏为是为了帮助转换旧脚本和习惯。它是脆弱的,即将更改,因此不应依赖。
如果你运⾏ps -aux >/dev/null,那么你就会得到下⾯这⾏警告信息
Warning: bad ps syntax, perhaps a bogus '-'? See procps.sf/faq.html
综上:使⽤时两者之间直接选择ps aux
2. ps aux 和ps -ef
两者的输出结果差别不⼤,但展⽰风格不同。aux是BSD风格,-ef是System V风格。这是次要的区别,⼀个影响使⽤的区别是aux会截断command列,⽽-ef不会。当结合grep时这种区别会影响到结果。
综上:以上三个命令推荐使⽤:ps –ef