ps命令应用(查看进程)

记录:320

场景:在CentOS 7.9操作系统上,ps命令是查看进程信息工具。查看进程状态、进程使用内存状况、进程使用CPU状况、进程PID等。

版本:

操作系统:CentOS 7.9

1.ps命令介绍

ps命令,process status的简称。一个查看进程信息工具。可以查看启动哪些进程、进程运行的状态、进程占用资源情况等。收集这些信息有助于监测和控制进程。比如,写自动化脚本时,根据进程信息决定下一步执行动作。

ps命令,查看进程执行瞬间的进程信息工具。查看进程状态、进程使用内存状况、进程使用CPU状况、进程PID等。

top命令,查看进程实时动态信息工具。查看进程状态、进程使用内存状况、进程使用CPU状况、进程PID等。

僵尸进程,一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中。这种进程称为僵尸进程。

2.ps常用命令

(1)ps帮助命令

命令:ps --help a

功能:查看ps支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

(2)查看当前会话中打开进程

命令:ps

功能:查看当前会话打开的进程,实际上就是ps进程和bash进程。因为ps是使用bash的shell脚本,因此也有bash进程。

(3)查看全部进程

命令:ps -e

命令:ps -A

功能:-e,-A,查看全部进程。打印字段:PID、TTY、TIME、CMD信息。一般使用-e。

(4)查看全部进程且完整格式,包括命令行

命令:ps -ef

功能:-f完整格式,包括命令行。打印字段:UID、PID、PPID、C、STIME、TTY、TIME、CMD。

(5)查看全部进程搜索指定内容

命令:ps -ef | grep java

功能:在ps -ef列出的内容中,使用grep命令搜索含有关键字java的进程,实际一般这样组合使用。注意,使用搜索后,不显示字段名称了。

(6)查看全部进程显示完整信息

命令:ps -eF

功能:-F,显示完整信息,打印字段:UID、PID、PPID、C、SZ、RSS、PSR、STIME、TTY、TIME、CMD。如果想查看进程对内存和CPU使用情况,可以使用此命令。

(7)查看全部进程以用户维度输出信息

命令:ps aux

功能:a,打印全部有tty终端启动的进程。x,打印没有tty控制终端的进程。u,以用户为导向的格式输出。打印字段:USER、PID、%CPU、%MEM、VSZ、RSS、TTY、STAT、STATT、TIME、COMMAND。需要查看用户的进程的CPU、内存以及进程状态,可以使用这种方式

(8)查看全部进程以用户维度输出信息指定搜索

命令:ps aux | grep java

功能:查看全部进程以用户维度输出信息指定关键字搜索。

3.ps命令字段解析

执行ps命令后,打印信息的第一行是字段名称,即每一列代表的意思。

3.1常用字段

UID:进程的用户的ID号。

USER:启动进程的用户。

PID:进程的唯一ID。

PPID:进程的父进程的ID号。

C:进程的CPU处理器利用率。

TTY:启动进程的终端名称。表示该进程在哪个终端运行,不是从某个终端启动的进程或者与终端无关的进程显示为?号,查看终端名称可以使用:ll /dev/。

TIME:进程使用CPU的累计时间。

CMD/COMMAND:进程所运行的命令。

STIME/STATT:进程的启动时间。

%CPU:进程占用CPU的百分比

%MEM:进程占用内存的百分比(使用物理内存计算)。

VSZ:进程占用的虚拟内存大小(单位:KB)

RSS:进程占用的实际内存大小(单位:KB)

PSR:当前进程被调度到的CUP核序号。

SZ:进程在物理页面中的核心镜像的大小。

STAT:进程当前状态。

3.2字段STAT说明

STAT:进程当前状态。STAT字段每个字母代表意思。

R:running,运行或可运行状态。

S:interruptable sleeping,可中断睡眠(等待事件完成),正在睡眠的进程。

D:uninterruptable sleeping,不可中断的睡眠进程。

T:stopped,停止或被追踪的进程。

Z:zombie,僵死进程。

s:session leader,会话层状态。

N:低优先级进程。

<:高优先级进程。

+:前台进程。

l:多线程进程。(字母l)。

3.ps和其它几个命令

查看进程:ps -ef

查看CPU信息:cat /proc/cpuinfo

查看内信息:free -h

查看文件系统信息:df -h

查看目录和文件占用空间信息:du -h

查看磁盘挂载信息:lsblk

4.帮助命令列表

命令:ps --help a

查看ps 支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

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

以上,感谢。

2022年11月18日

猜你喜欢

转载自blog.csdn.net/zhangbeizhen18/article/details/127928609