【Linux】命令 - ps(查看进程信息)

Posted by 西维蜀黍 on 2021-03-07, Last Modified on 2022-12-10

linux上进程有5种状态:

  • 运行(正在运行或在运行队列中等待)
  • 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)
  • 不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)
  • 僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)
  • 停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行)

ps工具标识进程的5种状态码:

  • D 不可中断 uninterruptible sleep (usually IO)
  • R 运行 runnable (on run queue)
  • S 中断 sleeping
  • T 停止 traced or stopped
  • Z 僵死 a defunct (”zombie”) process

ps

In most Unix and Unix-like operating systems, the ps program (short for “process status”) displays the currently-running processes. A related Unix utility named top provides a real-time view of the running processes.

命令参数

  • -a :显示所有进程(Display information about other users’ processes as well as your own. This will skip any processes which do not have a controlling terminal, unless the -x option is also specified.)

  • -A:Display information about other users’ processes, including those without controlling terminals.

  • -c 只显示进程的可执行名称(默认情况下,会显示该进程启动时,attch上的所有参数)(Change the ``command’’ column output to just contain the executable name, rather than the full command line.)

  • -N 反向选择

  • -E 显示每个进程执行时的环境变量

    $ ps -E
      PID TTY           TIME CMD
    83362 ttys000    0:00.06 /Applications/iTerm.app/Contents/MacOS/iTerm2 --server login -fp wei.shi TERM_SESSION_ID=w0t0p1:741916E9-C8FA-41DD-BDF2-C0AC2DBB6B12 SSH_AUTH_SOCK=/private/tmp/com.a
    83367 ttys000    0:00.26 -zsh TERM_SESSION_ID=w0t0p1:741916E9-C8FA-41DD-BDF2-C0AC2DBB6B12 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.cDMObZjFUz/Listeners LC_TERMINAL_VERSION=3.3.4 Apple_Pu
    74895 ttys002    1:15.11 /private/var/folders/gb/jjnm5jv56wv9j15djld4f77sxf66ph/T/___server_main PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Applications/Wireshark.a
    81859 ttys003    0:00.04 /Applications/iTerm.app/Contents/MacOS/iTerm2 --server login -fp wei.shi TERM_SESSION_ID=w0t0p0:3A8836BA-23DD-49B9-AD79-11BE500CECED SSH_AUTH_SOCK=/private/tmp/com.a
    
  • -f 显示进程之间的继承关系(Display the uid, pid, parent pid, recent CPU usage, process start time, controlling tty, elapsed CPUusage, and the associated command. If the -u option is also used, display the user name rather then the numeric uid. When -o or -O is used to add to the display following -f, the command field is not truncated as severely as it is in other formats.)

  • -H 显示树状结构

  • r 显示当前终端的进程

  • T 显示当前终端的所有程序

  • -u 指定用户的所有进程

  • -au 显示较详细的资讯

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

  • -C <命令> 列出指定命令的状况

  • --lines<行数> 每页显示的行数

  • --width<字符数> 每页显示的字符数

  • --help 显示帮助信息

  • --version 显示版本显示

列含义

     %cpu       percentage CPU usage (alias pcpu)
		 %mem       percentage memory usage (alias pmem)
     acflag     accounting flag (alias acflg)
     args       command and arguments
     comm       command
     command    command and arguments
     cpu        short-term CPU usage factor (for scheduling)
     etime      elapsed running time
     flags      the process flags, in hexadecimal (alias f)
     gid        processes group id (alias group)
     inblk      total blocks read (alias inblock)
     jobc       job control count
     ktrace     tracing flags
     ktracep    tracing vnode
     lim        memoryuse limit
     logname    login name of user who started the session
     lstart     time started
     majflt     total page faults
     minflt     total page reclaims
     msgrcv     total messages received (reads from pipes/sockets)
     msgsnd     total messages sent (writes on pipes/sockets)
     nice       nice value (alias ni)
     nivcsw     total involuntary context switches
     nsigs      total signals taken (alias nsignals)
     nswap      total swaps in/out
     nvcsw      total voluntary context switches
     nwchan     wait channel (as an address)
     oublk      total blocks written (alias oublock)
     p_ru       resource usage (valid only for zombie)
     paddr      swap address
     pagein     pageins (same as majflt)
     pgid       process group number
     pid        process ID
     ppid       parent process ID
     pri        scheduling priority
     re         core residency time (in seconds; 127 = infinity)
     rgid       real group ID
     rss        resident set size
     ruid       real user ID
     ruser      user name (from ruid)
     sess       session ID
     sig        pending signals (alias pending)
     sigmask    blocked signals (alias blocked)
     sl         sleep time (in seconds; 127 = infinity)
     start      time started
     state      symbolic process state (alias stat)
     svgid      saved gid from a setgid executable
     svuid      saved UID from a setuid executable
     tdev       control terminal device number
     time       accumulated CPU time, user + system (alias cputime)
     tpgid      control terminal process group ID
     tsess      control terminal session ID
     tsiz       text size (in Kbytes)
     tt         control terminal name (two letter abbreviation)
     tty        full name of control terminal
     ucomm      name to be used for accounting
     uid        effective user ID
     upr        scheduling priority on return from system call (alias usrpri)
     user       user name (from UID)
     utime      user CPU time (alias putime)
     vsz        virtual size in Kbytes (alias vsize)
     wchan      wait channel (as a symbolic name)
     wq         total number of workqueue threads
     wqb        number of blocked workqueue threads
     wqr        number of running workqueue threads
     wql        workqueue limit status (C = constrained thread limit, T = total thread limit)
     xstat      exit or stop status (valid only for stopped or zombie process)

Demo

显示所有进程

# List all running processes:
$ ps aux

显示指定用户信息

  • -u [userlist]: Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist.
    • The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to U and –user.
$ ps -u root

# 显示当前用户
$ ps --user $(id -u)

显示命令执行时的命令参数(e.g., 环境变量)

$ ps -ef

# e.g.
$ ps -ef
root        2748     921  0 17:06 ?        00:00:00 /snap/docker/1767/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 6379 -container-ip 172.17.0.2 -container-port 6379
root         958       1  0 17:06 ?        00:00:07 java -Xmx512M -Xms512M -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlin
...

# List all running processes including the full command string:
$ ps auxww

$ ps eww [pid]
# e.g.,
$ ps eww 21530
  PID   TT  STAT      TIME COMMAND
21530 s001  S+     0:00.01 ./a.out TERM_SESSION_ID=w0t1p0:A303486F-BF80-4546-84E2-99859FED4FEF SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.RKk8eQB5sE/Listeners HSTR_CONFIG=hicolor SW=aaa _=/Users/wei.shi/Downloads/./a.out

按照资源使用排序

  • --sort spec: Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,…]]. Choose a multi-letter key from the STANDARD FORMAT SPECIFIERS section. The “+” is optional since default direction is increasing numerical or lexicographic order. Identical to k. For example: psjax –sort=uid,-ppid,+pid
# 根据 CPU 使用来升序排序
$ ps -aux --sort -pcpu

# 根据 内存使用 来升序排序
$ ps -aux --sort -pmem

显示进程父子关系

可以看到,多了一列 PPID,用于指明当前进程的父进程 PID。

$ ps axjf

$ ps -ejH

# macOS下可用
$ pstree

# Get the parent PID of a process:
$ ps -o ppid= -p pid

$ ps -ef

根据 PID/进程名称显示特定进程

  • -p pidlist: Select by PID. This selects the processes whose process ID numbers appear in pidlist. Identical to p and –pid.
  • -C cmdlist: Select by command name. This selects the processes whose executable name is given in cmdlist.
$ ps -p 42

$ ps -C getty

显示线程信息

$ ps -eLf

$ ps axms

Reference