您的位置:首页 > 其它

ps命令用法详解(转)

2016-03-12 00:56 281 查看
ps是Linux下用来查看进程的各种信息的工具,通过ps命令我们可以非常清楚地掌握各个进程所使用的系统资源,及时发现那些资源消耗异常的进程。保障服务器系统的高可用性及稳定性。

系统环境:Debian GNU/Linux 7.7 (wheezy)
pathname:/bin/ps
package:procps

ps命令选项:

简单进程选择:

-A 选择所有进程
-e 选择所有进程,等同于-A

通过清单选择进程(PROCESS SELECTION BY LIST)

-C cmdlist
通过进程的可执行文件的名字来选择进程

输出格式控制:

-l —— 长格式,-y选项和这个一起用时很有用
-o —— 自定义输出格式,后接格式说明符,(格式说明符间用逗号隔开)
-y —— 不显示flags,显示rss来代替addr,这个选项只能和-l选项一起用

输出修改器(OUTPUT MODIFIERS):

--no-headers —— 不打印头部
--sort spec —— 指定排序次序,默认是按spec(说明符)升序排列(+spec:按spec升序排列;-spec,按spec降序排列)

标准格式说明符:

代码头部描述
%mem%MEM进程占用物理内存的百分比
argsCOMMAND输出命令及其全部参数
cmdCMDalias args
commCOMMAND仅输出可执行文件的名字
commandCOMMANDalias args
etimeELAPSED输出进程的运行时间,时间格式如下:[[dd-]hh:]mm:ss(运行时间没有超过一天则不显示dd-,如果运行时间还不到一小时,则显示格式为mm:ss)
euserEUSER输出进程的执行者的用户名(有时是显示执行者的uid)
lstartSTARTED输出进程启动时刻
pidPID输出进程的进程号
psrPSR进程当前被分配的处理器
pmem%MEMalias %mem
rssRSS物理内存的使用大小(单位为KB)
rssizeRSSalias rss
rszRSZalias rss
sS最小状态显示(只显示一个字符)
statSTAT多字符状态显示(显示更详细的进程状态信息)
stateSalias s
unameUSERalias euser
userUSERalias euser
进程状态代码:
D 不可中断睡眠
R 正在运行或可运行(或者位于运行队列中)
S 可中断睡眠
T 已终止
X 已死(should never be seen)
Z 已故进程,已终止但还未被其父进程回收
< 高优先级(对其它用户不友好)
N 低优先级(对其它用户友好)
L 页面锁定在内存
s 含有子进程(is a session leader)
l 多线程
+ 位于前台进程组中

root@godontop:~# ps -eo rss,pid,user,etime,lstart,stat,args --no-headers |sort -k 1 -nr
按第一个参数rss(物理内存)的使用情况从大到小排序

按RSS的大小升序排列
# ps -lyC php5-fpm --sort rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 0 18303 1 0 80 0 508 10460 - ? 00:00:03 php5-fpm
S 33 22675 18303 1 80 0 39040 18523 - ? 00:00:04 php5-fpm
S 33 22676 18303 0 80 0 42576 19404 - ? 00:00:03 php5-fpm
S 33 22435 18303 0 80 0 47904 20698 - ? 00:00:19 php5-fpm

备注:当-o选项与--no-headers一起使用时,--no-headers需放在格式说明符的后面
http://godontop.com/linux-ps-command-usage-69/
ps -eLo pid,tid,class,rtprio,ni,pri,psr,pcpu,pmem,stat,wchan:30,comm
-e 显示所有进程
-L 是现实线程信息
-o 表示使用用户定义格式打印信息

tid 表示线程id
pcpu 表示cpu使用率

pmem 参照%cpu
%cpu %CPU cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a
percentage. It will not add up to 100% unless you are lucky. (alias pcpu).
pmem 参照%mem
%mem %MEM ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. (alias pmem).

wchan:30 WCHAN name of the kernel function in which the process is sleeping, a "-" if the process is running, or a "*" if the process is multi-threaded and ps is not displaying threads.

程序状态字段解释:

D Uninterruptible sleep (usually IO) 不可中断睡眠
R Running or runnable (on run queue) 正在执行或可执行,表示目前在运行队列里面
S Interruptible sleep (waiting for an event to complete) 可中断睡眠
T Stopped, either by a job control signal or because it is being traced.停止
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.僵尸进程

For BSD formats and when the stat keyword is used, additional characters may be displayed:附加字段
< high-priority (not nice to other users) 高优先级
N low-priority (nice to other users) 低优先级
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group
http://blog.chinaunix.net/uid-21706718-id-3341751.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: