linux ps - processes

【基本介绍】
ps - report a snapshot of the current processes.
ps displays information about a selection of the active processes。
ps显示与进程相关的信息。

【基本参数】

-a Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
a Lift the BSD-style "only yourself" restriction,
-e Select all processes. Identical to -A.
-C cmdlist Select by command name.
-U userlist select by real user ID (RUID) or name.
-p pidlist Select by PID.
-f does full-format listing. This option can be combined with many other UNIX-style options to add additional columns.
-o format user-defined format.
-L Show threads, possibly with LWP and NLWP columns
--sort spec specify sorting order
args COMMAND command with all its arguments as a string.
rss RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes)



【简单实例】
1. Display all processes(显示所有进程)
$ ps ax
$ ps -ef


2. Display process by user(显示对应用户的进程)
[code="linux"]
# ps -u root
PID TTY TIME CMD
1 ? 00:00:28 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 migration/0
4 ? 00:00:08 ksoftirqd/0
5 ? 00:00:00 migration/0
6 ? 00:00:02 watchdog/0
7 ? 00:10:16 events/0
8 ? 00:00:00 cgroup
9 ? 00:00:00 khelper
10 ? 00:00:00 netns
11 ? 00:00:00 async/mgr
12 ? 00:00:00 pm
13 ? 00:00:05 sync_supers
[/code]

3. Show process by name or process id(显示命令对应的进程)

$ ps -C apache2
PID TTY TIME CMD
2359 ? 00:00:00 apache2
4524 ? 00:00:00 apache2
4525 ? 00:00:00 apache2



4. Sort process by cpu or memory usage(针对列进行排序)

$ ps aux --sort=-pcpu | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 2.6 0.7 51396 7644 ? Ss 02:02 0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root 1249 2.6 3.0 355800 30896 tty1 Rsl+ 02:02 0:02 /usr/bin/X -background none :0 vt01 -nolisten tcp
root 508 2.4 1.6 248488 16776 ? Ss 02:02 0:03 /usr/bin/python /usr/sbin/firewalld --nofork
silver 1525 2.1 2.3 448568 24392 ? S 02:03 0:01 /usr/bin/python /usr/share/system-config-printer/applet.py


5. Display threads of a process(显示进程的线程)
[code="linux"]
# ps -p 38027 -L
PID LWP TTY TIME CMD
38027 38027 ? 00:00:15 memcached
38027 38029 ? 00:00:00 memcached
38027 38030 ? 00:00:00 memcached
38027 38031 ? 00:00:00 memcached
[/code]

6. Change the columns to display(格式化初始列)

$ ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm
PID USERNAME CPU_USAGE %MEM COMMAND
1 root 0.0 0.0 init
2 root 0.0 0.0 kthreadd
3 root 0.0 0.0 ksoftirqd/0
4 root 0.0 0.0 kworker/0:0
5 root 0.0 0.0 kworker/0:0H



7. Display elapsed time of processes(显示运行时间)
[code="linux"]
# ps -e -o pid,comm,etime
PID COMMAND ELAPSED
1 init 11-22:57:09
2 kthreadd 11-22:57:09
3 migration/0 11-22:57:09
4 ksoftirqd/0 11-22:57:09
5 migration/0 11-22:57:09
6 watchdog/0 11-22:57:09
[/code]

8. Turn ps into an realtime process viewer(实时查看进程,可以用top)
[code="linux"]
# watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'

Every 1.0s: ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15 Mon Oct 20 14:16:32 2014

PID USER CMD %MEM %CPU
41664 root watch -n 1 ps -e -o pid,una 0.2 0.3
2051 mysql /usr/libexec/mysqld --based 2.6 0.2
1 root /sbin/init 0.1 0.0
2 root [kthreadd] 0.0 0.0[/code]

9. Finding memory Leak (ps –sort pmem)

$ ps ev --pid=27645
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32

if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.如果RSS数值在变大,占用的MEM百分比也在变大,可以作为内存泄漏的依据.

【参考引用】
[url]http://www.binarytides.com/linux-ps-command/[/url]
[url]http://www.thegeekstuff.com/2011/04/ps-command-examples/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值