One Linux command a day (35): ps command

Posted by vfwood on Thu, 04 Nov 2021 17:51:22 +0100

This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog https://www.joshua317.com/article/184

1, Introduction

The ps command in Linux system is used to display the process status of the current system and list the currently running processes in the system.

ps command is the most basic and powerful process viewing command. You can use this command to determine which processes are running and running, whether the process is over, whether the process is dead, which processes occupy too many resources, etc. in short, most of the information can be obtained by executing this command.

ps command provides a one-time view of the process, and the view results provided by it are not dynamic and continuous; If you want to monitor the process time, you should use the top tool.

Extension:

Five states of processes on linux:

1. function(Running or waiting in the run queue) 

2. interrupt(Dormant, Blocked, Waiting for a condition to form or receive a signal) 

3. Non interruptible(No wake-up and no operation after receiving the signal, The process must wait until an interrupt occurs) 

4. dead(process aborted, But the process descriptor does not exist, Until the parent process calls wait4()Release after system call) 

5. stop it(Process received SIGSTOP, SIGSTP, SIGTIN, SIGTOU Stop operation after signal) 

The ps command identifies five status codes of a process:

D Non interruptible uninterruptible sleep (usually IO) 

R function runnable (on run queue) 

S interrupt sleeping 

T stop it traced or stopped 

Z dead a defunct ("zombie") process 

2, Format description

ps [options]
ps[parameter]

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).

3, Option description

-a	Displays the programs executed under all terminals, except the phase job leader
a	Displays all programs under the current terminal, including those of other users
-A	Show all programs
-c	display CLS and PRI Field
c	When programs are listed, the real instruction name of each program is displayed, excluding the path, option or resident service identification
-C <Instruction name>	Specifies the name of the instruction and lists the status of the program that executes the instruction
-d	Show all procedures, but not those of the phase leader
-e	Effect and assignment of this option A"Same options
e	When programs are listed, the environment variables used by each program are displayed
-f	display UID,PPIP,C And STIME Field
f	use ASCII Character display tree structure to express the relationship between programs
-g <group name >	Effect and assignment of this option-G"If the options are the same, you can also use the name of the phase leader
g	Displays all programs under the current terminal, including those of group leaders
-G <Group ID >	Lists the status of programs that belong to the group, or you can specify them using the group name
h	Do not display header columns
-H	Displays a tree structure that represents the relationship between programs
-j or j	Display the program status in the format of work control
-l or l	Use a detailed format to display program status
L	List the relevant information of the field
-m or m	Show all threads
n	Expressed in numbers USER and WCHAN Field
-N	Displays all programs except execution ps Outside the program under the instruction terminal
-p <Program identification code>	Specify the program ID and list the status of the program
p <Program identification code>	Effect and assignment of this option-p"The options are the same, with only slight differences in list format
r	Only the programs that the row terminal is executing are listed
-s <Stage operation>	Specify the program identification code of the phase and list the status of the program belonging to the phase
s	The program status is displayed in the format of program signal
S	When listing programs, include interrupted subroutine data
-t <Terminal number>	Specify the terminal number and list the status of the program belonging to the terminal
t <Terminal number>	Effect and assignment of this option-t"The options are the same, with only slight differences in list format
-T	Displays all programs under the current terminal
-u <User identification code>	Effect and assignment of this option-U"Same options
u	Display program status in a user dominated format
-U <User identification code>	Lists the status of programs belonging to the user, or you can use the user name to specify
U <User name>	Lists the status of programs that belong to this user
v	Displays 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	Old style Linux i386 Display program status in login format
-y	Fit options-l"When in use, it is not displayed F(flag)Field, and RSS Field substitution ADDR Field
-- <Program identification code>	Effect and assignment of this option p"Same options
--cols <Characters per column>	Sets the maximum number of characters per column
--columns <Characters per column>	Effect and assignment of this option--cols"Same options
--cumulative	Effect and assignment of this option S"Same options
--deselect	Effect and assignment of this option-N"Same options
--forest	Effect and assignment of this option f"Same options
--headers	Repeat title column
--help	Online help
--info	Display troubleshooting information
--lines <Show number of columns>	Set the number of columns of the display
--no-headers	Effect and assignment of this option h"The options are the same, with only slight differences in list format
--group <group name >	Effect and assignment of this option-G"Same options
--Group <Group ID >	Effect and assignment of this option-G"Same options
--pid <Program identification code>	Effect and assignment of this option-p"Same options
--rows <Show number of columns>	Effect and assignment of this option--lines"Same options
--sid <Stage operation>	Effect and assignment of this option-s"Same options
-tty <Terminal number>	Effect and assignment of this option-t"Same options
--user <User name>	Effect and assignment of this option-U"Same options
--User <User identification code>	Effect and assignment of this option-U"Same options
--version	Effect and assignment of this option-V"Same options
--widty <Characters per column>	Effect and assignment of this option-cols"Same options

Common parameters, usually as long as you master the common parameters.

a  Show all processes

-a Display all programs under the same terminal

-A Show all processes

c  Displays the real name of the process

-N Reverse selection

-e Equals“-A"

e  Show environment variables

f  Show relationships between programs

-H Display tree structure

r  Displays the process of the current terminal

T  Displays all programs of the current terminal

u  Specify all processes for the user

-au Show more detailed information

-aux Displays all trips that include other users 

-C<command> Lists the status of the specified command

--lines<Number of rows> Number of rows displayed per page

--width<Number of characters> Number of characters displayed per page

--help display help information

--version Display version display

4, Command function

Used to display the current process status

5, Common usage

5.1 display all processes

# ps -aux
#perhaps
# ps -A
#perhaps
# ps -ef

 

 

 

5.2 display all processes and output them to ps.txt file:

# ps -aux > ps.txt

5.3 find a specific process through the combination of ps and grep

# ps -ef | grep php-fpm

 

 

 

5.4 display the process of the specified user

# ps -u root

5.5 sort processes by CPU resource usage

# ps aux | sort -nk 3

5.6 sort the processes according to the usage of memory resources

# ps aux | sort -rnk 4 

5.7 list the PID and relevant information that belong to you at present

# ps -l

 

 

 

F Flag representing this program (flag), 4 The representative user is super userS Represents the status of this program (STAT),About each STAT The meaning of will be introduced in the text UID The program is UID Owned PID That's the program ID !PPID Is the name of its parent program IDC CPU Percentage of resources used PRI This is Priority (Priority execution order) The abbreviation of is described in detail later NI This is Nice Value, which we will continue to introduce in the next section ADDR This is kernel function,Indicates the portion of the program in memory. If it's a running The procedure is generally "-"SZ Used memory size WCHAN Is this procedure currently in operation? If yes - Indicates that it is in operation TTY Terminal location of the login TIME Used CPU Time. CMD What are the instructions given

5.8 all programs currently in memory

# ps aux
[root@service-01 ~]# ps auxUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot         1  0.0  0.0 191092  3668 ?        Ss   Mar23 163:50 /usr/lib/systemd/systemd --switched-root --system --deserialroot         2  0.0  0.0      0     0 ?        S    Mar23   0:37 [kthreadd]root         4  0.0  0.0      0     0 ?        S<   Mar23   0:00 [kworker/0:0H]root         6  0.0  0.0      0     0 ?        S    Mar23  11:47 [ksoftirqd/0]root         7  0.0  0.0      0     0 ?        S    Mar23   2:54 [migration/0]root         8  0.0  0.0      0     0 ?        S    Mar23   0:00 [rcu_bh]

 

 

 

USER: Should process Belonging to that user account PID : Should process Number of%CPU: Should process Used CPU Percentage of resources%MEM: Should process Percentage of physical memory used VSZ : Should process Amount of virtual memory used (Kbytes)RSS : Should process Fixed amount of memory occupied (Kbytes)TTY : Should process It operates on that terminal. If it has nothing to do with the terminal, it will be displayed ?,In addition, tty1-tty6 Is the login program on this machine. If it is pts/0 And so on, it is expressed as a program connected to the host by the network. STAT: The current status of the program, the main status are    D : Sleep state that cannot be interrupted (usually IO Process of)	R : The program is currently in operation or can be operated	S : The program is currently sleeping (It can be said that idle state),But it can be detected by some signals (signal) Wake up.	T : The program is currently detecting or stopping	Z : The program should have been terminated, but its parent program cannot normally terminate it, resulting in zombie (Xinjiang corpse) Status of the program START: Should process Time triggered to start TIME : Should process Actual use CPU Time of operation COMMAND: The actual instructions of the program

5.9 display in a process tree like manner

ps -axjf

 

 

 

 

This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog https://www.joshua317.com/article/184

Topics: Linux