Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1339046
  • 博文数量: 166
  • 博客积分: 46
  • 博客等级: 民兵
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-11 13:45
个人简介

现任职北京某互联网公司运维经理,高级架构师,涉足互联网运维行业已经超过10年。曾服务于京东商城,互动百科等互联网公司,早期运维界新星。 长期专研,C语言开发,操作系统内核,大型互联网架构。http://www.bdkyr.com

文章分类

分类: 系统运维

2013-10-14 11:02:30

一、ps命令用途介绍  

    显示瞬间行程 (process) 的动态

 

二、语法: 

    ps [参数]

三、参数选项说明:

********* simple selection ********* 

-A all processes  /列出所有的进程                   

-N negate selection                 

-d all except session leaders        

-e all processes   /显示所有进程                  

-T  all processes on this terminal   /显示当前终端的所有进程

-a  all w/ tty, including other users  //显示终端上的所有进程,包括其他用户的进程。

g  OBSOLETE -- DO NOT USE            

r  only running processes 前面没有-/显示运行中的进程;           

x  processes w/o controlling ttys     / x 显示无控制终端的进程;

-au 显示较详细的资讯

-aux 显示所有包含其他使用者的行程      ★★★★★常用

********* selection by list *********

-C by command name

-G by real group ID (supports names)

-U by real user ID (supports names)

-g by session OR by effective group name

-p by process ID

-s processes in the sessions given

-t by tty

-u by effective user ID (supports names) /按用户名和启动时间的顺序来显示进程;

U  processes for specified users

t  by tty

 

*********** output format ********** 

-o,o user-defined 

-f full     /全格式     

f 用树形格式来显示进程;

-j,j job control   /用任务格式来显示进程;

s  signal         

-O,O preloaded -o 

v  virtual memory 

-l,l long        / 长格式输出;

u  user-oriented  

-F   extra full   

X  registers      

-h 不显示标题

*********** long options ***********

--Group

--User

--pid

--cols

–ppid

--group

--user

 --sid

--rows

 –info

--cumulative

--format

--deselect

--sort

--tty

--forest

--version

--heading

 --no-heading

–context

 

********* 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

 

四、命令实际各个参数的使用案例及拓展。

    范例1.

[root@backup ~]# ps

     PID TTY          TIME CMD

    4137 pts/0    00:00:00 bash

    4170 pts/0    00:00:00 ps
可以看到,显示地项目共分为四项,依次为PID(进程ID)TTY(终端名称)

TIME(进程执行时间)COMMAND(该进程地命令行输入).

    范例2. –A列出所有的行程

[root@backup ~]# ps –A  #列出所有的行程

    PID TTY          TIME CMD

      1 ?        00:00:02 init

      2 ?        00:00:00 migration/0

      3 ?        00:00:00 ksoftirqd/0

      4 ?        00:00:00 watchdog/0

      5 ?        00:00:00 events/0

      6 ?        00:00:00 khelper

      7 ?        00:00:00 kthread

      ……       ………… …………

      4137 pts/0    00:00:00 bash

      4165 pts/0    00:00:00 ps

范例3.  -a  显示终端上的所有进程,包括其他用户的进程。

    [root@backup ~]# ps -a  #显示终端上的所有进程,包括其他用户的进程。

    PID TTY          TIME CMD

    4462 pts/1    00:00:00 tail #会话窗口2上运行的命令 tail -f /var/log/messages

    4463 pts/0    00:00:00 ps   #会话窗口1上运行的 ps -a

范例4.u

[root@backup ~]# ps -A u

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.2   2160   644 ?        Ss   07:59   0:02 init [3]                 

root         2  0.0  0.0      0     0 ?        S<   07:59   0:00 [migration/0]

root         3  0.0  0.0      0     0 ?        SN   07:59   0:00 [ksoftirqd/0]

root         4  0.0  0.0      0     0 ?        S<   07:59   0:00 [watchdog/0]

root         5  0.0  0.0      0     0 ?        S<   07:59   0:00 [events/0]

root         6  0.0  0.0      0     0 ?        S<   07:59   0:00 [khelper]

root         7  0.0  0.0      0     0 ?        S<   07:59   0:00 [kthread]

root        10  0.0  0.0      0     0 ?        S<   07:59   0:00 [kblockd/0]

root        11  0.0  0.0      0     0 ?        S<   07:59   0:00 [kacpid]

root       176  0.0  0.0      0     0 ?        S<   07:59   0:00 [cqueue/0]

root       179  0.0  0.0      0     0 ?        S<   07:59   0:00 [khubd]

root       181  0.0  0.0      0     0 ?        S<   07:59   0:00 [kseriod]

root       245  0.0  0.0      0     0 ?        S    07:59   0:00 [khungtaskd]

root       246  0.0  0.0      0     0 ?        S    07:59   0:00 [pdflush]

root       247  0.0  0.0      0     0 ?        S    07:59   0:00 [pdflush]

root       248  0.0  0.0      0     0 ?        S<   07:59   0:00 [kswapd0]

root       249  0.0  0.0      0     0 ?        S<   07:59   0:00 [aio/0]

root       472  0.0  0.0      0     0 ?        S<   07:59   0:00 [kpsmoused]

root       496  0.0  0.0      0     0 ?        S<   07:59   0:00 [ata/0]

root       497  0.0  0.0      0     0 ?        S<   07:59   0:00 [ata_aux]

root       508  0.0  0.0      0     0 ?        S<   07:59   0:00 [mpt_poll_0]

root       509  0.0  0.0      0     0 ?        S<   07:59   0:00 [mpt/0]

root       510  0.0  0.0      0     0 ?        S<   07:59   0:00 [scsi_eh_0]

root       515  0.0  0.0      0     0 ?        S<   07:59   0:00 [kstriped]

范例4.-N #显示所有的程序,除了执行ps指令终端机下的程序之外。

[root@backup ~]# ps -N   #显示所有的程序,除了执行ps指令终端机下的程序之外。

  PID TTY          TIME CMD

    1 ?        00:00:02 init

    2 ?        00:00:00 migration/0

    3 ?        00:00:00 ksoftirqd/0

    4 ?        00:00:00 watchdog/0

    5 ?        00:00:00 events/0

    6 ?        00:00:00 khelper

    7 ?        00:00:00 kthread

   10 ?        00:00:00 kblockd/0

   11 ?        00:00:00 kacpid

  176 ?        00:00:00 cqueue/0

  179 ?        00:00:00 khubd

  181 ?        00:00:00 kseriod

  245 ?        00:00:00 khungtaskd

  246 ?        00:00:00 pdflush

  247 ?        00:00:00 pdflush

  248 ?        00:00:00 kswapd0

  249 ?        00:00:00 aio/0

  472 ?        00:00:00 kpsmoused

  496 ?        00:00:00 ata/0

  497 ?        00:00:00 ata_aux

  508 ?        00:00:00 mpt_poll_0

  509 ?        00:00:00 mpt/0

  510 ?        00:00:00 scsi_eh_0

  515 ?        00:00:00 kstriped

  524 ?        00:00:00 kjournald

  549 ?        00:00:00 kauditd

  582 ?        00:00:00 udevd

 1645 ?        00:00:00 kgameportd

 1818 ?        00:00:00 kmpathd/0

 1819 ?        00:00:00 kmpath_handlerd

 1843 ?        00:00:00 kjournald

 2300 ?        00:00:00 auditd

 2302 ?        00:00:00 audispd

 2332 ?        00:00:00 syslogd

 2335 ?        00:00:00 klogd

 2368 ?        00:00:00 portmap

 2398 ?        00:00:00 rpciod/0

 2404 ?        00:00:00 rpc.statd

 2436 ?        00:00:00 rpc.idmapd

 2460 ?        00:00:00 dbus-daemon

 2483 ?        00:00:00 hcid

 2496 ?        00:00:00 sdpd

 2507 ?        00:00:00 krfcommd

 2551 ?        00:00:00 pcscd

 2565 ?        00:00:00 acpid

 2581 ?        00:00:01 hald

 2582 ?        00:00:00 hald-runner

 2590 ?        00:00:00 hald-addon-acpi

 2596 ?        00:00:00 hald-addon-keyb

 2605 ?        00:00:00 hald-addon-stor

 2626 ?        00:00:00 hidd

 2650 ?        00:00:00 automount

 2696 ?        00:00:00 sshd

 2717 ?        00:00:00 cupsd

 2734 ?        00:00:00 xinetd

 2746 ?        00:00:00 mysqld_safe

 2778 ?        00:00:00 mysqld

 2820 ?        00:00:00 sendmail

 2828 ?        00:00:00 sendmail

 2842 ?        00:00:00 gpm

 2855 ?        00:00:00 crond

 2882 ?        00:00:00 xfs

 2895 ?        00:00:00 anacron

 2907 ?        00:00:00 atd

 2933 ?        00:00:00 avahi-daemon

 2934 ?        00:00:00 avahi-daemon

 2965 ?        00:00:00 smartd

 2968 tty1     00:00:00 mingetty

 2969 tty2     00:00:00 mingetty

 2970 tty3     00:00:00 mingetty

 2971 tty4     00:00:00 mingetty

 2972 tty5     00:00:00 mingetty

 2973 tty6     00:00:00 mingetty

 3024 ?        00:00:00 yum-updatesd

 3026 ?        00:00:00 gam_server

 3027 ?        00:00:00 sshd

3029 pts/0    00:00:00 bash

3076 pts/0    00:00:00 ps

说明:两行红色的属于执行ps指令终端机下的程序,只有在使用-A参数时,才显示。

范例5. -u #查看进程所有者及其他少许详细信息
    [root@backup ~]# ps u

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root      2904  0.0  0.1   1748   468 tty2     Ss+  00:19   0:00 /sbin/mingetty tty2

root      2905  0.0  0.1   1748   468 tty3     Ss+  00:19   0:00 /sbin/mingetty tty3

root      2906  0.0  0.1   1748   468 tty4     Ss+  00:19   0:00 /sbin/mingetty tty4

root      2907  0.0  0.1   1748   464 tty5     Ss+  00:19   0:00 /sbin/mingetty tty5

root      2908  0.0  0.1   1748   468 tty6     Ss+  00:19   0:00 /sbin/mingetty tty6

root      2968  0.0  0.5   5496  1416 pts/0    Ss   00:22   0:00 -bash

root      3004  0.0  0.5   5496  1432 tty1     Ss+  00:26   0:00 -bash

root      3100  0.0  0.3   2272   820 pts/0    R+   00:51   0:00 ps u

bash进程前面有条横线,意味着该进程便是用户地登录shell,所以对于一个登录用户来说带短横线地进程只有一个.还可以看到%CPU%MEM两个选项,前者指该进程占用地CPU时间和总时间地百分比;后者指该进程占用地内存和总内存地百分比.在这种情况下看到了所有控制终端地进程;当然对于其他那些没有控制终端地进程还是没有观察到,所以这时就需要运用x选项.运用x选项可以观察到所有地进程情况.

范例6 aux

[root@backup ~]# ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.2   2160   644 ?        Ss   07:59   0:02 init [3]                 

root         2  0.0  0.0      0     0 ?        S<   07:59   0:00 [migration/0]

root         3  0.0  0.0      0     0 ?        SN   07:59   0:00 [ksoftirqd/0]

root         4  0.0  0.0      0     0 ?        S<   07:59   0:00 [watchdog/0]

root         5  0.0  0.0      0     0 ?        S<   07:59   0:00 [events/0]

root         6  0.0  0.0      0     0 ?        S<   07:59   0:00 [khelper]

root         7  0.0  0.0      0     0 ?        S<   07:59   0:00 [kthread]

root        10  0.0  0.0      0     0 ?        S<   07:59   0:00 [kblockd/0]

中间省略了一部分

root      3045  0.0  1.1  10208  2904 ?        Ss   08:15   0:00 sshd: root@pts/0

root      3047  0.0  0.5   5500  1416 pts/0    Ss   08:16   0:00 -bash

root      3799  0.0  5.9  27712 15292 ?        SN   10:16   0:00 /usr/bin/python -tt /usr/libexec

root      3925  0.0  0.3   2276   824 pts/0    R+   10:53   0:00 ps aux

 

说明:

USER: 进程所有者
PID:
进程ID

%CPU: 占用的 CPU 使用率
%MEM:
占用的内存使用率
VSZ:
占用的虚拟内存大小
RSS:
占用的内存大小

TTY: 终端的次要装置号码 (minor device number of tty)

STAT: 进程状态:

 

[root@backup ~]# ps aux |grep sshd

root      2698  0.0  0.4   7192  1060 ?        Ss   00:19   0:00 /usr/sbin/sshd

root      2966  0.0  1.1  10044  2888 ?        Ss   00:22   0:00 sshd: root@pts/0

root      3129  0.0  0.1   1928   504 pts/0    R+   01:02   0:00 grep sshd

START: 启动进程的时间;
TIME:
进程消耗CPU的时间;
COMMAND:
命令的名称和参数;

最常用的方法是ps aux,然后再用管道符号导向到grep去查找特定的进程,然后再对特定的进程进行操作。

[root@backup ~]# ps aux |grep sshd

root      2698  0.0  0.4   7192  1060 ?        Ss   00:19   0:00 /usr/sbin/sshd

root      2966  0.0  1.1  10044  2888 ?        Ss   00:22   0:00 sshd: root@pts/0

root      3129  0.0  0.1   1928   504 pts/0    R+   01:02   0:00 grep sshd

范例7查看当前系统进程的uid,pid,stat,pri, uid号排序.

[root@backup ~]# ps -eo pid,stat,pri,uid --sort uid

  PID STAT PRI   UID

    1 Ss    21     0

    2 S<   139     0

    3 SN     5     0

    4 S<   139     0

    5 S<    29     0

    6 S<    29     0

    7 S<    29     0

   10 S<    29     0

   11 S<    19     0

  176 S<    28     0

  179 S<    29     0

  181 S<    29     0

  245 S     23     0

  246 S     23     0

  247 S     24     0

  248 S<    28     0

  249 S<    28     0

  472 S<    27     0

  496 S<    24     0

  497 S<    24     0

  508 S<    29     0

  509 S<    19     0

  510 S<    19     0

  515 S<    19     0

  524 S<    29     0

  549 S<    28     0

  582 S   18     0

 1539 S<    24     0

 1820 S<    19     0

 1821 S<    19     0

 1845 S<    24     0

 2302 S  28     0

 2304 S  31     0

 2334 Ss    21     0

 2337 Ss    15     0

 2400 S<    27     0

 2438 Ss    15     0

 2485 Ss    16     0

 2489 Ss    15     0

 2509 S<    30     0

 2553 Ssl   14     0

 2567 Ss    21     0

 2585 S     14     0

 2607 S     21     0

 2628 Ss    15     0

 2652 Ssl   16     0

 2698 Ss    21     0

 2719 Ss    21     0

 2736 Ss    14     0

 2758 Ss    21     0

 2780 Ss    24     0

 2793 Ss    21     0

 2833 SNs    0     0

 2845 Ss    21     0

 2900 S     16     0

 2903 Ss    23     0

 2904 Ss+   21     0

 2905 Ss+   20     0

 2906 Ss+   19     0

 2907 Ss+   18     0

 2908 Ss+   17     0

 2959 SN     5     0

 2961 SN     5     0

 2966 Ss    24     0

 2968 Ss    24     0

 3004 Ss+   24     0

 3133 R+    24     0

 2406 Ss    14    29

 2370 Ss    18    32

 2820 Ss    21    43

 2766 Ss    14    51

 2584 Ss    24    68

 2592 S     21    68

 2598 S     24    68

 2871 Ss    24    70

 2872 Ss    17    70

 2462 Ss    24    81

阅读(3968) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~