ps command application (view process)

Records : 320

Scenario : On the CentOS 7.9 operating system, the ps command is a tool for viewing process information. View process status, process memory usage, process CPU usage, process PID, etc.

Version:

Operating system: CentOS 7.9

1. Introduction to the ps command

ps command, short for process status. A tool for viewing process information. You can view which processes are started, the running status of the processes, and the resources occupied by the processes. Gathering this information helps monitor and control the process. For example, when writing an automation script, the next step is determined based on process information.

The ps command is used to view the process information tool at the moment of process execution. View process status, process memory usage, process CPU usage, process PID, etc.

The top command is a tool to view the real-time dynamic information of the process. View process status, process memory usage, process CPU usage, process PID, etc.

Zombie process, a process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the state information of the child process, the process descriptor of the child process is still saved in the system. Such a process is called a zombie process.

2. ps common commands

(1) ps help command

Command: ps --help a

Function: View all commands and options supported by ps. In actual work, viewing this manual should be a must.

(2) View open processes in the current session

Command: ps

Function: View the processes opened by the current session, which are actually the ps process and the bash process. Since ps is a shell script using bash, there is also a bash process.

(3) View all processes

Command: ps -e

Command: ps -A

Function: -e, -A, view all processes. Print fields: PID, TTY, TIME, CMD information. Generally use -e.

(4) View all processes and complete formats, including command lines

Command: ps -ef

Function: -f full format, including the command line. Print fields: UID, PID, PPID, C, STIME, TTY, TIME, CMD.

(5) View all processes and search for specified content

Command: ps -ef | grep java

Function: In the content listed by ps -ef, use the grep command to search for processes containing the keyword java, which is generally used in combination. Note that after using the search, the field name is not displayed.

(6) View all processes to display complete information

Command: ps -eF

Function: -F, display complete information, print fields: UID, PID, PPID, C, SZ, RSS, PSR, STIME, TTY, TIME, CMD. If you want to view the memory and CPU usage of the process, you can use this command.

(7) View all processes to output information in the user dimension

Command: ps aux

Function: a, print all processes started by tty terminal. x, print processes that do not have a tty controlling terminal. u, output in a user-oriented format. Print fields: USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, STATT, TIME, COMMAND. Need to view the CPU, memory and process status of the user's process, you can use this method

(8) View all processes to output information in the user dimension to specify the search

Command: ps aux | grep java

Function: View all processes and output information in the user dimension to search for specified keywords.

3. ps command field analysis

After executing the ps command, the first line of the printed information is the field name, that is, the meaning of each column.

3.1 Common fields

UID: The ID number of the user of the process.

USER: The user who started the process.

PID: The unique ID of the process.

PPID: The ID number of the parent process of the process.

C: Process CPU processor utilization.

TTY: The name of the terminal that started the process. Indicates which terminal the process is running on. Processes not started from a certain terminal or processes not related to the terminal are displayed as ?. To view the terminal name, you can use: ll /dev/.

TIME: The cumulative time the process uses the CPU.

CMD/COMMAND: The command run by the process.

STIME/STATT: The start time of the process.

%CPU: The percentage of CPU occupied by the process

%MEM: The percentage of memory occupied by the process (calculated using physical memory).

VSZ: The size of virtual memory occupied by the process (unit: KB)

RSS: the actual memory size occupied by the process (unit: KB)

PSR: The serial number of the CPU core to which the current process is scheduled.

SZ: The size of the core image of the process in physical pages.

STAT: The current state of the process.

3.2 Description of field STAT

STAT: The current state of the process. Each letter in the STAT field represents a meaning.

R: running, running or runnable state.

S: interruptable sleeping, which can interrupt sleep (wait for the event to complete), the process that is sleeping.

D: uninterruptable sleeping, an uninterruptible sleep process.

T: stopped, stopped or tracked process.

Z: zombie, zombie process.

s: session leader, session layer status.

N: Low priority process.

<: High priority process.

+: foreground process.

l: multi-threaded process. (letter l).

3.ps and several other commands

View process: ps -ef

View CPU information: cat /proc/cpuinfo

View internal information: free -h

View file system information: df -h

View directory and file space information: du -h

View disk mount information: lsblk

4. List of help commands

Command: ps --help a

View all commands and options supported by ps. In actual work, viewing this manual should be a must.

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

Above, thanks.

November 18, 2022

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/127928609