linux系统命令ps实例讲解

简介: 一、ps命令用途介绍       显示瞬间行程 (process) 的动态   二、语法:      ps [参数] 三、参数选项说明: ********* simple selectio...

一、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

目录
相关文章
|
20天前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
244 1
二、Linux文本处理与文件操作核心命令
|
5天前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
79 2
|
15天前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
56 0
Linux内存问题排查命令详解
|
20天前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
458 2
|
1月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
116 16
|
27天前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
2月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
425 0
|
2月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
126 0
|
2月前
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
3月前
|
Linux C++
每天一个linux命令(8):cp 命令
cp 命令是 Linux 中用于复制文件或目录的命令。它的名字来源于英文单词 copy。这个命令非常常用,特别是在需要备份文件或创建文件副本时。
144 0

热门文章

最新文章