整理ps使用笔记

尽管使用ps只需要记住常用命令: ps aux ps -ef
并且理解输出的列含义即可

但不理解命令的含义,用起来总有种空虚感

下面研究一下

介绍

造成ps命令参数混乱的原因是它兼容了三种命令行规范:

  • BSD —— <option>
  • UNIX —— -<option>
  • GNU —— --<option>

通过help命令,可以看到ps似乎对于options做了分类

$ ps --help

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

BSD

那么就从BSD开始,按分类看参数作用

默认

默认:只看当前终端所有进程

$ sleep 99999999 &
[1] 42621
$ ps
    PID TTY          TIME CMD
  34397 pts/5    00:00:00 bash
  42621 pts/5    00:00:00 sleep
  42622 pts/5    00:00:00 ps

simple

$ ps --help simple

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

For more details see ps(1).

a

a: 看所有终端(包括其他用户)的所有进程(tty only)

其中,多了一些列

扫描二维码关注公众号,回复: 14574331 查看本文章
  • COMMAND —— 显示中多了参数
  • STAT —— 进程状态 R=运行中S=等待s 表示管理着会话+ 表示是前台进程(可接受前台输入)
$ ps a
    PID TTY      STAT   TIME COMMAND
    985 ttyS0    Ss+    0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
    986 tty1     Ss+    0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
  13520 pts/0    Ss     0:00 -bash
  13582 pts/0    S      0:00 sleep 9999999
  13620 pts/0    R+     0:00 ps a

$ ps au
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         985  0.0  0.1 225432  2144 ttyS0    Ss+  Mar26   0:00 /sbin/agetty -o -p -- \u --keep-bau
root         986  0.0  0.0 225792  1448 tty1     Ss+  Mar26   0:00 /sbin/agetty -o -p -- \u --noclear
student    13520  0.0  0.2 234216  5104 pts/0    Ss   04:49   0:00 -bash
student    13582  0.0  0.0 217044   728 pts/0    S    04:58   0:00 sleep 9999999
student    13622  0.0  0.2 267612  3892 pts/0    R+   05:38   0:00 ps au

x

显示当前用户的进程

其中 tty=? 的是没有终端的进程(真正的后台进程)

$ ps
    PID TTY          TIME CMD
  13520 pts/0    00:00:00 bash
  13582 pts/0    00:00:00 sleep
  13643 pts/0    00:00:00 bash
  14001 pts/0    00:00:00 ps
$ ps x
    PID TTY      STAT   TIME COMMAND
  13508 ?        Ss     0:00 /usr/lib/systemd/systemd --user
  13513 ?        S      0:00 (sd-pam)
  13519 ?        S      0:03 sshd: student@pts/0
  13520 pts/0    Ss     0:00 -bash
  13582 pts/0    S      0:00 sleep 9999999
  13643 pts/0    S      0:00 bash
  14002 pts/0    R+     0:00 ps x
$ ps xu
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
student    13508  0.0  0.5  93740  9524 ?        Ss   04:49   0:00 /usr/lib/systemd/systemd --user
student    13513  0.0  0.2 253164  4992 ?        S    04:49   0:00 (sd-pam)
student    13519  0.0  0.3 161876  6656 ?        R    04:49   0:03 sshd: student@pts/0
student    13520  0.0  0.2 234216  5108 pts/0    Ss   04:49   0:00 -bash
student    13582  0.0  0.0 217044   728 pts/0    S    04:58   0:00 sleep 9999999
student    13643  0.0  0.2 234376  5472 pts/0    S    05:49   0:00 bash
student    14003  0.0  0.2 267612  3792 pts/0    R+   06:36   0:00 ps xu
$ ps xul
error: conflicting format options
$ ps xl
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4  1000   13508       1  20   0  93740  9524 do_epo Ss   ?          0:00 /usr/lib/systemd/systemd --us
5  1000   13513   13508  20   0 253164  4992 -      S    ?          0:00 (sd-pam)
5  1000   13519   13503  20   0 161876  6656 -      S    ?          0:03 sshd: student@pts/0
0  1000   13520   13519  20   0 234216  5108 -      Ss   pts/0      0:00 -bash
0  1000   13582   13520  20   0 217044   728 hrtime S    pts/0      0:00 sleep 9999999
0  1000   13643   13520  20   0 234376  5472 -      S    pts/0      0:00 bash
0  1000   14005   13643  20   0 255196  2108 -      R+   pts/0      0:00 ps xl
$ ps xlf
F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
5  1000   13519   13503  20   0 161876  6656 -      S    ?          0:03 sshd: student@pts/0
0  1000   13520   13519  20   0 234216  5108 -      Ss   pts/0      0:00  \_ -bash
0  1000   13582   13520  20   0 217044   728 hrtime S    pts/0      0:00      \_ sleep 9999999
0  1000   13643   13520  20   0 234376  5472 -      S    pts/0      0:00      \_ bash
0  1000   14006   13643  20   0 255196  2076 -      R+   pts/0      0:00          \_ ps xlf
4  1000   13508       1  20   0  93740  9524 do_epo Ss   ?          0:00 /usr/lib/systemd/systemd --us
5  1000   13513   13508  20   0 253164  4992 -      S    ?          0:00  \_ (sd-pam)

T、r

T: 显示当前终端的所有进程。
这输出结果和默认是一样的

$ ps T
    PID TTY      STAT   TIME COMMAND
  13520 pts/0    Ss     0:00 -bash
  13582 pts/0    S      0:00 sleep 9999999
  13634 pts/0    R+     0:00 ps T
$ ps t
    PID TTY      STAT   TIME COMMAND
  13520 pts/0    Ss     0:00 -bash
  13582 pts/0    S      0:00 sleep 9999999
  13635 pts/0    R+     0:00 ps t

r: 正在运行状态的进程。可以和a一起使用

$ ps r
    PID TTY      STAT   TIME COMMAND
  13975 pts/0    R+     0:00 ps r
$ cat testscript
#!/bin/bash
while true ; do
        echo "$@" >> test_output
#sleep 1
done
$ ~/testscript h &
[1] 13981
$ ps r
    PID TTY      STAT   TIME COMMAND
  13981 pts/0    R      0:01 /bin/bash /home/student/testscript h
  13982 pts/0    R+     0:00 ps r
$ ps ar
    PID TTY      STAT   TIME COMMAND
  13981 pts/0    R      0:07 /bin/bash /home/student/testscript h
  13984 pts/0    R+     0:00 ps ar
$ kill 13981
$ ps r
    PID TTY      STAT   TIME COMMAND
  13985 pts/0    R+     0:00 ps r
[1]+  Terminated              ~/testscript h

list

可以指定列出的范围
U <UID>t <tty>p <PID>q <PID>

$ ps --help list

Usage:
 ps [options]

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'

For more details see ps(1).

output

输出格式

$ ps --help output

Usage:
 ps [options]

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

For more details see ps(1).

对于BSD来说,有以下输出格式

  • f —— 树形关系展示(❗可以和下面格式一起使用)
    $ ps f
        PID TTY      STAT   TIME COMMAND
      13520 pts/0    Ss     0:00 -bash
      13582 pts/0    S      0:00  \_ sleep 9999999
      13643 pts/0    S      0:00  \_ bash
      14088 pts/0    R+     0:00      \_ ps f
    
  • j —— job控制格式
    $ ps j
       PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
      13519   13520   13520   13520 pts/0      14089 Ss    1000   0:00 -bash
      13520   13582   13582   13520 pts/0      14089 S     1000   0:00 sleep 9999999
      13520   13643   13643   13520 pts/0      14089 S     1000   0:00 bash
      13643   14089   14089   13520 pts/0      14089 R+    1000   0:00 ps j
    $ ps jf
       PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
      13519   13520   13520   13520 pts/0      14090 Ss    1000   0:00 -bash
      13520   13582   13582   13520 pts/0      14090 S     1000   0:00  \_ sleep 9999999
      13520   13643   13643   13520 pts/0      14090 S     1000   0:00  \_ bash
      13643   14090   14090   13520 pts/0      14090 R+    1000   0:00      \_ ps jf
    
  • l —— BSD long格式
    $ ps l
    F   UID     PID    PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
    0  1000   13520   13519  20   0 234216  5108 -      Ss   pts/0      0:00 -bash
    0  1000   13582   13520  20   0 217044   728 hrtime S    pts/0      0:00 sleep 9999999
    0  1000   13643   13520  20   0 234376  5472 -      S    pts/0      0:00 bash
    0  1000   14094   13643  20   0 255196  2072 -      R+   pts/0      0:00 ps l
    
  • s —— signal格式
    $ ps s
      UID     PID   PENDING   BLOCKED   IGNORED    CAUGHT STAT TTY        TIME COMMAND
     1000   13520  00000000  00010000  00380004  4b817efb Ss   pts/0      0:00 -bash
     1000   13582  00000000  00000000  00000000  00000000 S    pts/0      0:00 sleep 9999999
     1000   13643  00000000  00010000  00380004  4b817efb S    pts/0      0:00 bash
     1000   14095  00000000  00000000  00000000 <f3d1fef9 R+   pts/0      0:00 ps s
    
  • u —— 面向用户的格式(常用)
    $ ps u
    USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    student    13520  0.0  0.2 234216  5108 pts/0    Ss   04:49   0:00 -bash
    student    13582  0.0  0.0 217044   728 pts/0    S    04:58   0:00 sleep 9999999
    student    13643  0.0  0.2 234376  5472 pts/0    S    05:49   0:00 bash
    student    14096  0.0  0.2 267612  3888 pts/0    R+   07:21   0:00 ps u
    $ ps uf
    USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    student    13520  0.0  0.2 234216  5108 pts/0    Ss   04:49   0:00 -bash
    student    13582  0.0  0.0 217044   728 pts/0    S    04:58   0:00  \_ sleep 9999999
    student    13643  0.0  0.2 234376  5472 pts/0    S    05:49   0:00  \_ bash
    student    14098  0.0  0.2 267612  3924 pts/0    R+   07:22   0:00      \_ ps uf
    
  • v —— virtual memory格式
    $ ps v
        PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
      13520 pts/0    Ss     0:00      0  1053 233162 5108  0.2 -bash
      13582 pts/0    S      0:00      0    27 217016  728  0.0 sleep 9999999
      13643 pts/0    S      0:00      0  1053 233322 5472  0.2 bash
      14099 pts/0    R+     0:00      0   110 255085 2008  0.1 ps v
    
  • X —— register(寄存器)格式
    $ ps X
        PID           STACKP              ESP              EIP TMOUT ALARM STAT TTY        TIME COMMAND
      13520 00007ffc42cfeda0 0000000000000000 0000000000000000     -     - Ss   pts/0      0:00 -bash
      13582 00007fff5c7d2050 0000000000000000 0000000000000000     -     - S    pts/0      0:00 sleep 9999
      13643 00007ffc2b727060 0000000000000000 0000000000000000     -     - S    pts/0      0:00 bash
      14101 00007ffcb3cb4130 0000000000000000 0000000000000000     -     - R+   pts/0      0:00 ps X
    

总结

  • BSD
    • 注意分层

猜你喜欢

转载自blog.csdn.net/LawssssCat/article/details/129851174