TrumanWong

PS

Report the current system process status

Supplementary instructions

ps command is used to report the process status of the current system. You can use the kill command to interrupt and delete unnecessary programs at any time. The ps command is the most basic and also very powerful process viewing command. Use this command to determine which processes are running and running status, whether the process has ended, whether the process is zombie, which processes occupy too many resources, etc., in short Most of the information can be obtained by executing this command.

grammar

ps(option)

Options

-a: Displays programs executed under all terminals, except the stage job leader.
a: Display all programs under the current terminal, including programs of other users.
-A: Show all programs.
-c: Display CLS and PRI fields.
c: When listing programs, display the real command name of each program without including the path, options or identification of resident services.
-C<command name>: Specifies the name of the command to be executed and lists the status of the program for the command.
-d: Display all programs except the program of the stage job leader.
-e: This option has the same effect as specifying the "A" option.
e: When listing programs, display the environment variables used by each program.
-f: Display UID, PPIP, C and STIME fields.
f: Use ASCII characters to display the tree structure and express the relationship between programs.
-g<group name>: The effect of this option is the same as specifying the "-G" option. It can also be specified by the name of the stage job leader.
g: Display all programs under the current terminal, including the group leader's programs.
-G<Group ID>: Lists the status of programs belonging to this group, and can also be specified using the group name.
h: Do not display the title column.
-H: Display a tree structure, indicating the relationship between programs.
-j or j: Display program status in job control format.
-l or l: Use detailed format to display program status.
L: List the relevant information of the field.
-m or m: Display all execution threads.
n: Use numbers to represent USER and WCHAN fields.
-N: Display all programs, except programs under the terminal that execute the ps command.
-p<program identification code>: Specify the program identification code and list the status of the program.
p<program identification code>: The effect of this option is the same as specifying the "-p" option, with only slight differences in the list format.
r: List only the programs currently being executed by the terminal.
-s<stage operation>: Specify the program identification code of the stage operation and list the status of the programs belonging to the stage operation.
s: Use the program signal format to display the program status.
S: When listing programs, include interrupted subroutine information.
-t<Terminal number>: Specify the terminal number and list the status of the programs belonging to the terminal.
t<terminal number>: The effect of this option is the same as specifying the "-t" option, with only slight differences in the list format.
-T: Display all programs under the current terminal.
-u<User ID>: This option has the same effect as specifying the "-U" option.
u: Display program status in user-oriented format.
-U<user identification code>: Lists the status of programs belonging to this user, and can also be specified using the user name.
U<user name>: Lists the status of programs belonging to this user.
v: Display program status in virtual memory format.
-V or V: Display version information.
-w or w: Use a wide format to display program status. ​
x: Display all programs, not distinguished by terminal.
X: Use the old Linux i386 login format to display program status.
-y: When used with the option "-l", the F (flag) field is not displayed and the ADDR field is replaced by the RSS field.
-<Program ID>: This option has the same effect as specifying the "p" option.
--cols<number of characters per column>: Set the maximum number of characters per column.
--columns <number of characters per column>: This option has the same effect as specifying the "--cols" option.
--cumulative: This option has the same effect as specifying the "S" option.
--deselect: This option has the same effect as specifying the "-N" option.
--forest: This option has the same effect as specifying the "f" option.
--headers: Repeat the header column.
--help: online help.
--info: Display debugging information.
--lines<Number of display columns>: Set the number of columns in the display screen.
--no-headers: The effect of this option is the same as specifying the "h" option, with only slight differences in the list format.
--group<group name>: This option has the same effect as specifying the "-G" option.
--Group<Group ID>: This option has the same effect as specifying the "-G" option.
--pid <program identification code>: The effect of this option is the same as specifying the "-p" option.
--rows <number of columns to display>: This option has the same effect as specifying the "--lines" option.
--sid <stage job>: The effect of this option is the same as specifying the "-s" option.
--tty <terminal number>: This option has the same effect as specifying the "-t" option.
--user <user name>: This option has the same effect as specifying the "-U" option.
--User<User ID>: This option has the same effect as specifying the "-U" option.
--version: This option has the same effect as specifying the "-V" option.
--widty <number of characters per column>: This option has the same effect as specifying the "-cols" option.

Since the ps command can support quite a few system types, there are so many options!

Example

ps axo pid,comm,pcpu # View the PID, name and CPU usage of the process
ps aux | sort -rnk 4 # Sort processes by memory resource usage
ps aux | sort -nk 3 # Sort processes by CPU resource usage
ps -A # Display all process information
ps -u root # Display the specified user information
ps -efL # Check the number of threads
ps -e -o "%C : %p :%z : %a"|sort -k5 -nr # View processes and sort them by memory usage
ps -ef # Display all process information, together with the command line
ps -ef | grep ssh # Commonly used combination of ps and grep to find specific processes
ps -C nginx # Search for processes by name or command
ps aux --sort=-pcpu,+pmem # Sort by CPU or memory, -descending, +ascending
ps -f --forest -C nginx # Display the hierarchical relationship of the process in a tree style
ps -o pid,uname,comm -C nginx # Display the child processes of a parent process
ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm # Redefine labels
ps -e -o pid,comm,etime # Display the running time of the process
ps -aux | grep named # View named process details
ps -o command -p 91730 | sed -n 2p # Get the service name through the process id

List the PID and related information currently belonging to you for this login

ps -l
# UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD
# 501 566 559 4006 0 31 0 4317620 228 - Ss 0 ttys001 0:00.05 /App...cOS/iTerm2 --server /usr/bin/login -fpl kenny /Ap...s/MacOS/iTerm2 --launch_shel
# 501 592 577 4006 0 31 0 4297048 52 - S 0 ttys001 0:00.63 -zsh
  • F represents the flag of this program, 4 represents the user as super user
  • S represents the status of this program (STAT). The meaning of each STAT will be introduced in the text.
  • UID program is owned by this UID
  • PID is the ID of this program!
  • PPID is the ID of its superior parent program
  • Percentage of resources used by C CPU
  • PRI is the abbreviation of Priority (priority execution order), which will be introduced in detail later.
  • NI is the Nice value, which we will continue to introduce in the next section.
  • ADDR This is the kernel function, indicating which part of the memory the program is in. If it is a running program, it is usually "-"
  • Memory size used by SZ
  • WCHAN Is this program currently running? If it is - it means it is running.
  • TTY login's terminal location
  • TIME CPU time used.
  • What is the command issued by CMD?

By default, ps will only list the PID related to the current bash shell, so when I use ps -l, there are only three PIDs.

List all programs currently in memory

ps aux

# USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
# kenny 6155 21.3 1.7 7969944 284912 ?? S 203pm 199:14.14 /Appl...OS/WeChat
# kenny 559 20.4 0.8 4963740 138176 ?? S 203 PM 33:28.27 /Appl...S/iTerm2
# _windowserver 187 18.0 0.6 7005748 95884 ?? Ss 203 PM 288:44.97 /Syst...Light.WindowServer -daemon
# kenny 1408 10.7 2.1 5838592 347348 ?? S 203 pm 138:51.63 /Appl...nts/MacOS/Google Chrome
# kenny 327 5.8 0.5 5771984 79452 ?? S 203 PM 2:51.58 /Syst...pp/Contents/MacOS/Finder
  • USER: The process belongs to that user account
  • PID: the number of the process
  • %CPU: The percentage of CPU resources used by the process
  • %MEM: The percentage of physical memory occupied by the process
  • VSZ: The amount of virtual memory used by the process (Kbytes)
  • RSS: Fixed amount of memory (Kbytes) occupied by this process
  • TTY: Which terminal the process is running on. If it has nothing to do with the terminal, it will display?. In addition, tty1-tty6 are the login programs on the local machine. If it is pts/0, etc., it will show the reason. The program that connects the network to the host computer.
  • STAT: The current status of the program. The main statuses are
  • R: The program is currently running, or can be run -S: The program is currently sleeping (can be said to be in idle state), but can be awakened by certain signals.
  • T: The program is currently detecting or stopped.
  • Z: The program should have been terminated, but its parent program was unable to terminate it normally, resulting in a zombie program state.
  • START: The time when the process is triggered to start
  • TIME: The actual CPU usage time of the process
  • COMMAND: the actual instructions of the program

List programs display like program tree

ps -axjf

# USER               PID  PPID  PGID   SESS JOBC STAT   TT       TIME COMMAND            UID   C STIME   TTY
# root                 1     0     1      0    0 Ss     ??   10:51.90 /sbin/launchd        0   0 二03下午 ??
# root                50     1    50      0    0 Ss     ??    0:10.07 /usr/sbin/syslog     0   0 二03下午 ??
# root                51     1    51      0    0 Ss     ??    0:29.90 /usr/libexec/Use     0   0 二03下午 ??

Find the PID numbers associated with the cron and syslog services

ps aux | egrep '(cron|syslog)'

# root                50   0.0  0.0  4305532   1284   ??  Ss   二03下午   0:10.08 /usr/sbin/syslogd
# kenny            90167   0.0  0.0  4258468    184 s007  R+    9:23下午   0:00.00 egrep (cron|syslog)

Display all processes and output to ps001.txt file

ps -aux > ps001.txt

Output the specified fields