PS
Login Information
The commands executed below are all based on the above environment configuration.
Process Status
PS stands for Process Status.
Style | Source | Prefix |
---|---|---|
Unix | AT&T System of Bell Labs | - |
BSD | Berkeley Software Distribution | None |
GNU | Improved version by GNU Project | -- |
Due to historical reasons, the ps
command is mixed with various styles, and sometimes it looks like 💩.
Without Any Options
When no options are added, it will display all processes related to the current terminal.
In simple terms, if the TTY of the current terminal is pts/0
, it will display all processes with TTY as pts/0
.
This usually includes Shell processes, any processes started from this terminal (whether in the foreground or background), and the ps
command itself.
The sleep process above is started by me using the background process method.
Basic Options
Option: a
Display all processes related to the terminal, not just the current terminal or current user.
The above result is executed from the pts/0
terminal. You can see that the processes of the terminals ttyS0
, tty1
, pts/1
associated with the root user are all displayed,
and there is no distinction between users. The terminals pts/0
, ttyS0
, tty1
are all associated with the root user, and the terminal pts/1
is associated with the kuga user.
Option: u
Display process information in a user-friendly format.
If this option is only related to the display fields, then its process set should be the same as the ps
command without any options.
However, the above result shows that, in addition to the processes of the self terminal pts/0
, it also displays the processes of other terminals,
but it does not display the terminal process of pts/1
(user is kuga).
It can be concluded simply that the process set of the u
option is: all processes related to the current user terminal.
The above is all the terminal processes related to the root user. If you execute ps u
in the kuga user’s terminal,
you can get the following result, which is completely in line with expectations.
In addition, the definition of this process set is not unique to the u
option, it is related to the BSD style.
Option: x
Display all processes belonging to the current user.
The above result is executed in the kuga user’s terminal, so the number of processes will not be too many.
Option: -e
Obviously all processes.
Option: -f
Display process information in full format, can be used in combination with other UNIX-style options.
Option: -o
Customize the output fields, cannot be used in combination with the -f
and u
options.
Note that the uid here is different from the one displayed using the -f
option, here it is a numeric ID, but the -f
option will display the username.
Option: -p
Specify the process PID.
Option: –forest
Display the tree structure of processes and child processes in ASCII.
Process Sets
Different styles of options have different process sets, but as long as you observe carefully, you can still find some rules.
ps
: Without adding any options
ps -l
: Unix style, -l
means to display in long format.
ps l
: BSD style, l
means to display in BSD long format.
ps --forest
: GNU style, --forest
means to display in process tree.
In the above example, the options used are not related to the process set, and you can summarize the following rules:
Style | Process Set |
---|---|
None | Display all processes related to the current terminal |
Unix | Consistent with not adding options |
BSD | Display all processes related to the current user terminal |
GNU | Consistent with not adding options |
Obviously, Unix and GNU styles seem more logical, because their process sets are the same as when no options are added to the ps
command,
but the BSD style is playing its own game, including all processes related to the other terminals of the current user.
Different styles of options can be used together, but if two options both specify the display format, the execution will fail.
In addition, if multiple styles contain BSD style, but the process set is not specified in the options, then the BSD process set will be used by default.
Common Functions
List User Processes
List All Processes
The -e
option displays all processes, and the -f
option displays in full format, there is nothing special to say.
Strangely, the process set of aux
is different from the above analysis. From the above analysis, we know:
a
: Display all processes related to the terminal, not just the current terminal or current user.x
: Display all processes belonging to the current user.
Obviously, the sum of ax
is only all processes of the current user and all processes of the other terminals of the current user, but if you look at the official documentation.
When ax
is used together, it represents all processes, and we can also verify the number of processes.
You can see that the number of process sets of aux
and -ef
is the same, I am very detailed, I am talking about the number 🤪.
Rank by Memory Usage
go rocks
Special Fields
VSZ
Virtual Memory Size, in KB.
RSS
Resident Set Size, in KB.
TTY
The terminal associated with the process. If the process is associated with a certain terminal, it will display the name of the terminal; if it is not associated with a terminal, it will display ?
.
STAT
The process status code. Common status codes include:
R
: Running.S
: Sleeping, waiting for an event to complete.D
: Uninterruptible sleep, usually waiting for I/O.T
: Stopped or traced.Z
: Zombie process, the process has terminated but has not been cleaned up by the parent process.I
: Idle kernel thread.Ss
: The main process is in sleep state.R+
: Running process, and displayed in the foreground.
go rocks, need to understand different process states in depth
START
The time or date the process started. For a new process, it displays the time, and for an old process, it displays the date.
TIME
The cumulative CPU time the process has used, indicating how much CPU time the process has occupied in total.
C
The CPU usage of the process. This field displays the cumulative usage percentage of CPU time since the process started.