Linux ps
To monitor and control processes, you must first understand the current process, that is, you need to view the current process, and the ps command is the most basic and very powerful process viewing command. You can use this command to determine which processes are running and running, whether the process is terminated, whether the process is dead, and which processes are occupying excessive resources. In short, most of the information can be obtained by executing this command.
Ps command usage details
1. ps command and its parameters
The most common ps command is used to monitor the working conditions of background processes, because background processes do not communicate with standard input/output devices such as the screen keyboard, so if you need to detect the situation, you can use the ps command.
The syntax of this command is as follows:
PS [Option]
The following describes the Command Options:
-E: displays all processes.
-F full format.
-H does not display the title.
-L long format.
-W width output.
A displays all processes on the terminal, including those of other users.
R only displays running processes.
X shows the process without any control terminal.
O [+ |-] K1 [, [+ |-] K2 [,…] The process list is displayed based on the multilevel sorting sequence specified by the shortcut keys in short keys, K1, and K2.
The default sequence is specified for different PS formats. The default sequence can be overwritten by the user's specified sequence. The "+" character is optional, and the "-" character is used to reverse the direction of the specified key.
PIDs only lists the values marked as "8 then" and separated by commas. The process list must be followed by the last option of the command line parameter. spaces cannot be inserted in the middle. For example, PS-F1, 4, 5.
The following describes the long command line options, all of which start:
-- Sort X [+ |-] Key [, [+ |-] Key [,…] Select a multiple keys from the sort keys segment. The "+" character is optional, because the default direction is in ascending order of numbers or dictionary order. For example, PS-Jax-Sort = uid,-ppid, + PID.
-- Help: displays help information.
-- Version: displays the version information of the command.
The sort key is mentioned in the preceding option description, and further describes the sort key. Note that the values used in sorting are internal values used by PS, not only pseudo values in some output formats. The sorting Key List is shown in Table 4-3.
Table 4-3 sorting Key List
Short format |
Long format |
Description |
C |
CMD |
Simple executable name |
C |
Cmdline |
Complete command line |
F |
Flags |
Long MODE flag |
G |
Pgrp |
Group ID of the process |
G |
Tpgid |
Control tty Process Group ID |
J |
Cutime |
Cumulative user time |
J |
Cstime |
Accumulative system time |
K |
Utime |
User time |
K |
Stime |
System time |
M |
Min_flt |
Number of secondary page errors |
M |
Maj_flt |
Number of major page errors |
N |
Cmin_flt |
Accumulative secondary page Error |
N |
Cmaj_flt |
Accumulative Main Page Error |
O |
Session |
Dialog ID |
P |
PID |
Process ID |
P |
Ppid |
Parent process ID |
R |
RSS |
Resident size |
R |
Resident |
Resident page |
S |
Size |
Memory size (kilobytes) |
S |
Share |
Number of shared pages |
T |
Tty |
Tty secondary device number |
T |
Start_time |
Process start time |
U |
UID |
UID |
U |
User |
User Name |
V |
Vsize |
Total number of virtual memory (bytes) |
Y |
Priority |
Kernel scheduling priority |
2. Common PS Command Parameters
The parameters described in the previous two sections may make readers feel a little scary. In fact, this is a very easy-to-use command. Generally, users only need to master some of the most common command parameters.
The three most commonly used parameters are u, A, and X. The following uses an example to describe their usage.
[Example 20] log on to the system as root to view the current process status
$ PS
PID tty timecommand
5800 ttyp000: 00: 00 bash
5835 ttyp000: 00: 00 PS
The displayed items are divided into four parts: PID (process ID), tty (terminal name), and time (process execution time) command (command line input of the process ).
You can use the U option to view the process owner and other details, as shown below:
$ PS u
User PID % CPU % mem usz RSS tty stat Start Time Command
Test 5800 0.00.4 1892 1040 ttyp0 s nov27-Bash
Test 5836 0.00.3 2528 856 ttyp0 R nov27 PS u
There is a horizontal line in front of the bash process, which means that the process is the user's login shell, so for a login user, there is only one process with a short line. The options % CPU and % mem are also displayed. The former indicates the percentage of CPU time and total time occupied by the process, and the latter indicates the percentage of memory and total memory occupied by the process.
In this case, we can see all the processes that control the terminal. However, we still haven't observed other processes that do not control the terminal. Therefore, we need to use the X option. Use the X option to view all processes.
Ps command status description
Uppercase letters:
Duninterruptible sleep (usually Io)
R running orrunnable (on run Queue)
Sinterruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (shocould never be seen)
Z defunct ("zombie") process, terminated but notreaped by its parent.
Lowercase letters and other symbols:
<High-priority (not nice to other users)
Nlow-priority (nice to other users)
L has pageslocked into memory (for real-time and custom Io)
S is asession leader
L ismulti-threaded (using clone_thread, like nptl pthreads do)
+ Is in theforeground Process Group
The process has five statuses:
1. Run (running or waiting in the running Queue)
2. interruption (in sleep, blocked, waiting for the formation or receipt of a signal of a condition)
3. Do not interrupt (do not wake up when receiving the signal or do not run, the process must wait until there is an interruption)
4. Stiff (the process has been terminated, but the process descriptor exists until the parent process calls wait4 () and is released after the system call)
5. Stop (the process stops running after receiving signals from sigstop, sigstp, sigtin, and sigtou)
Five status codes used by the PS tool to identify a process:
D. uninterruptible sleep (usuallyio) cannot be interrupted)
R run runnable (on run Queue)
S interrupt sleeping
T stop traced or stopped
Z dead a defunct ("zombie") Process
Note: Other statuses include W (no resident page), <(high-priority process), n (low-priority process), and L (Memory Lock page ).
Use the PS format output to view the process status:
PS-eouser, stat..., cmd
User Username
UID user number
PID process number
Ppid parent process ID
Size memory size, Kbytes bytes.
Vsize Total virtual memory size, bytes (including code + Data + stack)
Total share pages
Nice process priority (default value: 0, maximum value:-20)
Priority (PRI) kernel scheduling priority
Percentage of physical memory shared by pmem Process
TRS Program Execution Code resident size
Total physical memory used by the RSS process, in Kbytes bytes
Time process execution till now the total CPU usage time
Stat Process status
CMD (ARGs) command execution in simple format
Example:
View the UID, PID, stat, PRI of the current system process, and sort them by uid.
PS-eopid, stat, PRI, uid -- Sort uid
# Ps-eopid, stat, PRI, uid -- Sort PID | HEAD
PID stat pri uid
1 ss 19 0
2 S 19 0
3 S 139 0
4 S 19 0
5 S 139 0
6 S 139 0
7 s 19 0
8 s 19 0
9 s 19 0
View the user, PID, stat, RSS, and ARGs of the current system process, and sort them by RSS.
PS-eouser, PID, stat, RSS, argS -- Sort RSS
# Ps-EO user, PID, stat, RSS, argS -- Sort RSS | HEAD
User PID stat RSS command
Root 2 S 0 [kthreadd]
Root 3 S 0 [migration/0]
Root 4 S 0 [ksoftirqd/0]
Root 5 S 0 [migration/0]
Root 6 S 0 [watchdog/0]
Root 7 S 0 [events/0]
Root 8 s 0 [cpuset]
Root 9 S 0 [khelper]
Root 10 s 0 [netns]