`

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(显示对应用户的进程)
[root@pandaVM ~]# 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


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(显示进程的线程)
[root@pandaVM ~]# 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


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(显示运行时间)
[root@pandaVM ~]# 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


8. Turn ps into an realtime process viewer(实时查看进程,可以用top)
[root@pandaVM ~]# 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


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百分比也在变大,可以作为内存泄漏的依据.

【参考引用】
http://www.binarytides.com/linux-ps-command/
http://www.thegeekstuff.com/2011/04/ps-command-examples/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics