ps详解(每周一个linux命令系列)(代码片段)

vinter vinter     2023-01-08     345

关键词:

(5)ps详解 (每周一个linux命令系列)

linux命令 ps详解


引言:今天的命令是用来看进程状态的ps命令

ps

我们先看man ps


 ps - report a snapshot of the current processes.

翻译:显示当前进程的快照。ps是 Process Status的缩写

具体的描述如下:


DESCRIPTION
       ps displays information about a selection of the active processes.  If you
       want a repetitive update of the selection and the displayed information, use
       top(1) instead.
       ps展示了当前运行的程序的一些选项信息快照,如果想要查看实时更新的程序信息,请使用top命令。

       This version of ps accepts several kinds of options:
       ps可以接收多种不同的选项
      
       
       1   UNIX options, which may be grouped and must be preceded by a dash.
       2   BSD options, which may be grouped and must not be used with a dash.
       2   GNU long options, which are preceded by two dashes.
       1   UNIX选项,需要分组使用,并且必须以破折号开头。
       2   BSD选项,需要分组使用,不得与破折号一起使用。
       3   GNU长选项,前面有两个破折号。

       Options of different types may be freely mixed, but conflicts can appear.
       There are some synonymous options, which are functionally identical, due to
       the many standards and ps implementations that this ps is compatible with.
        不同类型的选项可以自由混合,但可能出现冲突。
        由于ps兼容的许多标准和ps实现,有一些在功能上相同的同义选项。


       Note that "ps -aux" is distinct from "ps aux".  The POSIX and UNIX standards
       require that "ps -aux" print all processes owned by a user named "x", as well
       as printing all processes that would be selected by the -a option.  If the
       user named "x" does not exist, this ps may interpret the command as "ps aux"
       instead and print a warning.  This behavior is intended to aid in
       transitioning old scripts and habits.  It is fragile, subject to change, and
       thus should not be relied upon.
       请注意,“ps -aux”与“ps aux”不同。 
       POSIX和UNIX标准要求“ps -aux”打印名为“x”的用户拥有的所有进程,
       以及打印将由-a选项选择的所有进程。如果名为“x”的用户不存在,则此ps可能会将
       该命令解释为“ps aux”且打印警告。此行为旨在帮助转换旧脚本和习惯。
       这不一定是可靠的,尽量不要这样用

       By default, ps selects all processes with the same effective user ID
       (euid=EUID) as the current user and associated with the same terminal as the
       invoker.  It displays the process ID (pid=PID), the terminal associated with
       the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format
       (time=TIME), and the executable name (ucmd=CMD).  Output is unsorted by
       default.
       默认情况下,ps选择与当前用户具有相同有效用户ID(euid = EUID)且与调用者相同的终端关联的所有进程。
       它显示进程ID(pid = PID),与进程关联的终端(tname = TTY),
       累计CPU时间[DD-] hh:mm:ss格式(time = TIME)和可执行文件名(ucmd) = CMD)。
       默认情况下,不对输出排序。

       The use of BSD-style options will add process state (stat=STAT) to the
       default display and show the command args (args=COMMAND) instead of the
       executable name.  You can override this with the PS_FORMAT environment
       variable. The use of BSD-style options will also change the process selection
       to include processes on other terminals (TTYs) that are owned by you;
       alternately, this may be described as setting the selection to be the set of all processes filtered to exclude processes owned by other users or not on a terminal.  These effects are not considered when options are described as being "identical" below, so -M will be considered identical to Z and so on.
       使用BSD样式选项会将进程状态(stat = STAT)添加到默认显示,并显示命令args(args = COMMAND)
       而不是可执行文件名。您可以使用PS_FORMAT环境变量覆盖它。使用BSD风格的选项也会改变进程的选择,
       以包括您拥有的其他终端(TTY)上的进程;或者,这可以被描述为将选择过滤设置为排除其他用户所拥有的和
       不在终端上的进程的集合。当选项在下面被描述为“相同”时,不需要考虑这些效果,因此-M将被视为与Z相同
       依此类推。
       
       Except as described below, process selection options are additive.  The
       default selection is discarded, and then the selected processes are added to the set of processes to be displayed.  A process will thus be shown if it meets any of the given selection criteria.
       除以下描述外,进程选择选项是附加的。将丢弃默认选择,然后将所选进程添加到要显示的进程集。因此,如果满足任何给定的选择标准,将显示该进程。
       

我们再来看一下帮助文档

dev1:~$ ps --help all

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

Selection by list:
 -C <command>         command name
 -G, --Group <GID>    real group id or name
 -g, --group <group>  session or effective group name
 -p, p, --pid <PID>   process id
        --ppid <PID>  parent process id
 -q, q, --quick-pid <PID>
                      process id (quick mode)
 -s, --sid <session>  session id
 -t, t, --tty <tty>   terminal
 -u, U, --user <UID>  effective user id or name
 -U, --User <UID>     real user id or name

  The selection options take as their argument either:
    a comma-separated list e.g. ‘-u root,nobody‘ or
    a blank-separated list e.g. ‘-p 123 4567‘

Output formats:
 -F                   extra full
 -f                   full-format, including command lines
  f, --forest         ascii art process tree
 -H                   show process hierarchy
 -j                   jobs format
  j                   BSD job control format
 -l                   long format
  l                   BSD long format
 -M, Z                add security data (for SELinux)
 -O <format>          preloaded with default columns
  O <format>          as -O, with BSD personality
 -o, o, --format <format>
                      user-defined format
  s                   signal format
  u                   user-oriented format
  v                   virtual memory format
  X                   register format
 -y                   do not show flags, show rss vs. addr (used with -l)
     --context        display security context (for SELinux)
     --headers        repeat header lines, one per page
     --no-headers     do not print header at all
     --cols, --columns, --width <num>
                      set screen width
     --rows, --lines <num>
                      set screen height

Show threads:
  H                   as if they were processes
 -L                   possibly with LWP and NLWP columns
 -m, m                after processes
 -T                   possibly with SPID column

Miscellaneous options:
 -c                   show scheduling class with -l option
  c                   show true command name
  e                   show the environment after command
  k,    --sort        specify sort order as: [+|-]key[,[+|-]key[,...]]
  L                   show format specifiers
  n                   display numeric uid and wchan
  S,    --cumulative  include some dead child process data
 -y                   do not show flags, show rss (only with -l)
 -V, V, --version     display version information and exit
 -w, w                unlimited output width

        --help <simple|list|output|threads|misc|all>
                      display help and exit

打印所有用户的运行进程

a 显示所有用户的进程
u 显示进程的所有者
x 显示非终端启用的进程
注意之前介绍了 aux和-au的区别

输出:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     28726  0.0  0.0      0     0 ?        S    10:42   0:00 [kworker/3:0]
root     28889  0.0  0.0      0     0 ?        S    10:44   0:00 [kworker/0:2]
root     28979  0.0  0.0      0     0 ?        S    10:45   0:00 [kworker/2:2]
root     29014  0.0  0.0      0     0 ?        S    10:45   0:00 [kworker/1:0]

列属性说明 其他的列参考man ps吧

 - USER:该进程属于那个使用者账号的
 - PID :该进程的号码
 - %CPU:该进程所使用的CPU时间百分比。  
 - %MEM:该进程所使用的CPU时间百分比
 - VSZ :该进程占用的虚拟内存量 (Kbytes)
 - RSS :该进程占用的固定的内存量 (Kbytes)
 - TTY :该进程是在那个终端机上面运作,若与终端机无关,则显示 ?,
         另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0等等的,则表示为由网络连接进主机的程序。
 - STAT:Process Status  该程序目前的状态,主要的状态有
            D :不可中断的睡眠. 通常是处于I/O之中
            R :该程序目前正处于运行队列中.运行中/可运行.
            S :该程序目前正在睡眠当中,但可被某些事件或讯号 (signal) 触发唤醒。
            T :已停止. 可能是因为shell作业控制或进程正被调试.
            Z :该程序应该已经终止,但是其父程序没有终止他,造成 zombie (疆尸) 程序的状态
            注:进程状态还有其他的几种但是一般不常用
 - START    :该 process 被触发启动的时间
 - TIME     :该 process 实际使用 CPU 运作的时间
 - COMMAND  :该程序的实际指令

接下来说一些常用命令

最常用的就是和grep配合 查找特定的进程了

ps -ef | grep ssh

输出

root      1026     1  0 10月02 ?      00:00:08 /usr/sbin/sshd -D
root      1534  1026  0 10月08 ?      00:00:00 sshd: xxx [priv]
xxx  1536  1534  0 10月08 ?      00:00:01 sshd: [email protected]/1,pts/2,pts/3

定制输出的列属性

我们可以使用-o来定制列属性
ps -o uid,%cpu,%mem,pid,ppid,pgrp,session,tpgid,comm

查看特定用户的进程

在需要查看特定用户进程的情况下,我们可以使用 -u 参数。比如查看vinter用户的进程

ps -u vinter

对输出结果进行排序

升序和降序是通过在字段前面增加负号来设置的 有负号‘-’代表降序
ps --sort ppid 升序
ps --sort -ppid 降序

查看某个名称的进程

例如查看名为ps的进程

ps -C ps

查看某个PID的进程

ps -P 23178

把ps的结果输出到文件或分页查看

可以用 | 管道和 more 连接起来分页查看。
ps aux |more
把ps结果输出到ps.txt文本。
ps aux > ./ps.txt







每周一个linux命令之---uptime详解(代码片段)

每周一个linux命令之---uptime详解linux命令uptime详解引言:从今天开始,每周更新一个对程序员有用的linux命令,我真的没敢写每天一个,我怕我坚持不下去,每周一个还是可以的。既然每周一个了,就肯定不能拿ls,chmod这种命令... 查看详情

linux运维03:ps命令详解(代码片段)

 系列专题:Linux运维入门教程    Linux系统中会运行各种各样的进程,如果要对进程进行监控,首先必须了解当前进程的情况,也就是需要查看当前进程。ps命令就是最基本的进程查看命令,使用该命令可以... 查看详情

linux操作命令分类详解-进程查找其他(代码片段)

...量目录里查找文件5、其他命令系列文章Linux操作命令分类详解-命令速查(一ÿ 查看详情

linux中ps命令详解(代码片段)

文地址:http://blog.csdn.net/x_i_y_u_e/article/details/38708481 linux上进程有5种状态:  1.运行(正在运行或在运行队列中等待)2.中断(休眠中,受阻,在等待某个条件的形成或接受到信号)3.不可中断(收到信号不唤醒和不可运行,进程必须等待... 查看详情

linux操作命令分类详解-压缩备份(代码片段)

...解压命令2.5压缩解压命令总结系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限 查看详情

linux操作命令分类详解-进程查找其他(代码片段)

...量目录里查找文件5、其他命令系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限(三)Linux操作命令分类详解-压缩备份(四)Linu... 查看详情

(11)nc命令(每周一个linux命令)(代码片段)

nc(netcat)实用程序几乎可用于所有涉及TCP或UDP的事情。它可以打开TCP连接,发送UDP数据包,监听任意TCP和UDP端口,进行端口扫描,处理IPv4和IPv6。与telnet不同,nc可以很好地编写脚本,并将错误消息分离为标准错误,而不是像telnet... 查看详情

[转帖]ps命令详解(代码片段)

ps命令详解https://www.jianshu.com/p/cba22cce2f97 ps概述Linux中的ps命令是ProcessStatus的缩写。ps命令用来列出系统中当前运行的那些进程。ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显... 查看详情

精通linux系列服务器之间的telnet与scp命令用法,进程管理命令之ps-ef与psaux详解(代码片段)

🚀作者:“大数据小禅”🚀专栏简介:本专栏主要分享Linux技术,会涉及到常用的Linux命令操作,常用的服务部应用署以及相关运维知识,还有一些Linux系统的深层解析。🚀粉丝福利:加入小禅... 查看详情

linux操作命令分类详解-命令速查(代码片段)

...man命令(命令说明书)系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限(三)Linux操作命令分类详解-压缩备份(四)Linu... 查看详情

linux操作命令分类详解-用户权限(代码片段)

...nux附加权限)4.4.3对比范例系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限(三)Linux操作命令分类详解-压缩备份(四)Linu... 查看详情

linux运维09:ip命令详解(代码片段)

 系列专题:Linux运维入门教程     Linux中的ip命令功能强大,可以完成接口配置、路由管理等任务。 1.ip与ifconfig的区别    Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者。使用ip命令,... 查看详情

linux操作命令分类详解-压缩备份(代码片段)

...解压命令2.5压缩解压命令总结系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限(三)Linux操作命令分类详解-压缩备份(四)Linux... 查看详情

linux操作命令分类详解-目录文件(代码片段)

目录系列文章1、目录命令1.1 ls 列出目录及文件名1.2cd 切换目录1.3pwd显示当前目录1.4mkdir创建一个新的目录1.5rmdir删除一个空的目录1.6cp复制文件或目录1.7rm删除文件或目录1.8mv移动文件与目录,或修改文件与目录的名称2、文... 查看详情

linux操作命令分类详解-目录文件(代码片段)

...链接文件2.11 file识别文件类型系列文章Linux操作命令分类详解-命令速查(一)Linux操作命令分类详解-目录文件(二)Linux操作命令分类详解-用户权限(三)Linux操作命令分类详解-压缩备份(四)Linu... 查看详情

linux命令psaux|grepxxx详解(代码片段)

对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而ps命令(ProcessStatus)就是最基本同时也是非常强大的进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进... 查看详情

linux系列之查看进程线程的方法(代码片段)

...进程监控页面,但是在Linux系统查看进程一般使用top命令或者ps命令,但是如果要查看线程怎么查看?其实也可以使用这两个命令,所以本博客总结一下几种方法ps命令Linux的ps命令用于查看进程统计信息常用参数&#x... 查看详情

linux系列linux中dmesg命令详解(代码片段)

DATE:2019-5-15文章目录前言1、参考2、dmesg命令详解3、dmesg实例3.1、列出加载到内核中的所有驱动3.2、列出所有被检测到的硬件3.3、清空dmesg缓冲区日志3.4、实时监控dmesg日志输出前言  ‘dmesg’命令对设备故障的诊断是非常... 查看详情