Mastering Linux ps: Decode Options, Syntax, and Output Secrets
This comprehensive guide demystifies the Linux ps command by explaining its option syntax, the differences between BSD, SYSV and GNU styles, record‑selection rules, sorting, threading, field selection, modifiers, and error‑handling, while providing practical examples, visual illustrations, and tips for effective system monitoring.
1. The Programmer’s Dilemma
About ten years ago, while working as a product manager, I needed to learn common Linux commands from operations engineers. When using the ubiquitous
pscommand, I encountered a puzzling inconsistency: some engineers recommended
ps aux, while others suggested
ps -aux. Both seemed to produce the same output, yet older Linux versions complained about
ps -axuwith a "Warning: bad syntax" message, while
ps auxworked without error. This left me confused.
Later, after joining the ops team and studying the
psman page, I discovered that
ps auxis the correct usage. As I delved deeper, more questions arose, such as the meaning of the
%CPUcolumn, why it sometimes differs from
top, why
ps -eltruncates process names, and many others. After exploring the
pssource code, I compiled my findings into this document to help others.
2. Linux ps Option Syntax
The
pscommand is powerful and its options follow three syntax families:
BSD style – options must not start with a hyphen.
SYSV style – options start with a single hyphen.
GNU style – options start with a double hyphen.
Most long options have a short‑option equivalent. In CentOS 7 you can list them with
ps --help. This article focuses on BSD and SYSV styles; GNU long options are similar but usually require a parameter (e.g.,
--sid 1).
Options from different families can be mixed, but the command parses each token according to its family rules.
3. Record‑Selection Options
When using
ps, many engineers worry that
ps auxreturns far more rows than a plain
pscall. Understanding which options affect the number of records is essential.
3.1 all_processes option
Only two options display every process: the BSD‑style
aand the SYSV‑style
-e(or
-A). The
-eflag is easier to remember.
All processes are listed under
/proc; each numeric directory corresponds to a process. Running
ps -eyields a row count identical to the number of
/procentries.
3.2 simple_select options
Five simple‑select options exist (
-a,
-d,
a,
g,
x), comprising two SYSV and three BSD flags. Mixing SYSV with BSD flags causes an error, but options within the same family can be combined (e.g.,
-ad,
ga,
ax,
gx,
agx). BSD‑style simple‑select options receive two special treatments:
When BSD simple‑select is used, an extra
gflag is added.
If
a,
gand
xall appear, they are replaced by
-e.
Consequently,
ps auxis equivalent to
ps auxg,
ps agx u, or
ps -e u. In total there are six valid simple‑select combinations, each mapped to a bitmap value used for filtering.
3.3 selection_list options
These options filter processes based on a specific attribute (PID, session ID, UID, GID, command name, terminal, etc.) and each has a GNU‑style long counterpart. They often require a parameter (e.g.,
-p 1234).
Example usage:
<code>$ ps -u root -p 1</code>3.4 special‑select options
Option
hhides the header line.
Option
rlimits output to processes in running (R) or uninterruptible sleep (D) states.
Option
-Ndisplays the complement of the current selection (i.e., all processes not matched).
3.5 Record‑selection order
The selection proceeds in the following order:
Check for an all‑processes flag (
-e/
a).
If absent, evaluate simple‑select flags.
If still not selected, evaluate selection‑list flags.
If BSD flags are present, apply the BSD‑style
gbitmap rule.
If none of the above selected the record, apply a default bitmap
0xaa00(processes with matching tty and euid).
If
ris set, keep only R/D state records.
If
-Nis set, invert the selection.
Finally,
hremoves the header if requested.
4. Sorting Options
Three options control result ordering:
ksorts by one or more fields, with
+for ascending and
-for descending.
fproduces an ASCII‑tree based on parent‑child relationships.
-Hproduces a tab‑indented tree.
5. Thread‑Expansion Options
Processes may have multiple threads, visible under
/proc. The following options expand thread information:
H,
-L,
-T– show thread lines (record count differs from plain
ps -e).
M,
m,
-m– also include a summary line for the thread group.
Example:
ps -Llists each thread with its LWP (light‑weight process) ID; the group line shows the main process.
6. Field Options
6.1 General field‑group options
Different option groups produce different default column sets. For example,
ps auxshows USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND;
ps -elshows PID, PPID, WCHAN, TIME, CMD. The command can output up to 168 fields (see
ps L).
6.2 Specialized field‑group options
Job‑control format (
-j) – displays PPID, PID, PGID, SID, TPGID.
i386 register format (
-Z) – shows STACKP, ESP, EIP.
CPU‑signal format (
-s) – shows PENDING, BLOCKED, IGNORED, CAUGHT.
Virtual‑memory format (
-v) – shows MAJFL, TRS, DRS, RSS, %MEM.
SELinux label format (
-Z) – shows LABEL.
6.3 Custom field selection
Use
-o(or
o) to specify a comma‑separated list of fields, e.g.,
ps -o pid,cmd. The
Oor
-Ooptions prepend
pidand append
state,tty,time,commandto the custom list.
7. Field‑Modifier Options
Modifiers affect how a particular column is displayed:
w(repeatable) prevents truncation of long command lines.
cshortens the COMMAND column to the executable name.
eexpands COMMAND to include the environment.
Sincludes dead child processes in certain aggregate fields.
nshows numeric IDs instead of names (e.g., numeric UID, raw wchan value).
8. Common Fields Explained
8.1 Process‑ID fields
Key identifiers include
pid,
ppid,
pgid,
sid,
tpgid,
tid(thread ID),
nlwp(number of threads), and their aliases (
spid,
lwp,
tgid,
pgrp,
sess, etc.).
8.2 Command‑name fields
Three groups exist: short name (
comm/
ucomm), long name (
cmd/
command), and arguments (
args/
command). Short names are limited to 15 characters by the kernel.
8.3 Process‑state fields
Single‑byte fields
s/
stateand multi‑byte
statencode attributes such as nice value (
<for negative,
Nfor positive), memory lock (
L), session leader (
s), multithreaded (
l), foreground group (
+), etc.
8.4 Time fields
Fields like
lstart,
start_time,
etimesrecord start timestamps and elapsed time; they are convenient for scripting.
8.5 CPU‑time fields
Fields such as
cputime,
bsdtime,
pcpu,
creport CPU usage.
pcpuis a per‑thousand value;
cis a percentage (max 99). Multi‑threaded processes may show > 100%.
8.6 Memory fields
Key memory metrics include
vsz(virtual size, KB),
rss(resident set, KB),
pmem(% of total RAM),
trs(text),
drs(data),
size(swap),
minflt(minor page faults),
majflt(major page faults). Ratios such as
rss/minflt ≈ 4indicate normal page‑fault behavior.
8.7 Credential fields
Credentials include real UID/GID, effective UID/GID, saved set‑UID/GID, and filesystem UID/GID. They determine ownership and permission checks.
8.8 WCHAN fields
wchanand
wnameshow the kernel function where a sleeping process is blocked;
nwchanshows the raw pointer (truncated on 64‑bit systems).
9. Option Fault‑Tolerance
If a SYSV‑style combination fails to parse,
psattempts to reinterpret the same letters as BSD flags. For example,
ps -auxfalls back to
ps aux. Only seven letters (
-S,
-X,
-h,
-k,
-v,
-r,
-x) have BSD equivalents and can be auto‑corrected.
10. Practical Summary
10.1 Frequently Used Options
-e– show all processes.
h– hide the header.
k– sort by specified fields.
-L– expand threads.
-l– basic long format (s, pid, ppid, time, ucmd).
u– display CPU, memory, and RSS.
-o– custom field list.
The
pscommand offers 168 possible output fields; the most useful ones have been highlighted throughout this guide.
Original source: 值得收藏,史上最全Linux ps命令详解 , reposted from the AliDataOps public account, author Wen Maoquan.
Click the original article to visit the conference website.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.