ps takes a snapshot of processes at the moment it runs and prints a table. Unlike top, it doesn't refresh — but its output is easy to filter and parse in scripts.
Syntax
ps [options]
ps from procps has two option styles, and they can be mixed: UNIX — with a dash (ps -ef), BSD — without a dash (ps aux). The same letter means different things in them: ps -a and ps a are not the same. Without options ps shows only the processes of the current terminal.
Which processes to show
| Option | What it selects |
|---|
-e / -A | all processes |
ax | all processes (BSD style; a — all users, x — also those without a terminal) |
-p 1,42 / p 42 | by PID |
-C name | by executable name (exact match, no path) |
-u user | by effective user; -U — by real user |
-g / --sid / -s | by process group / by session |
--ppid 1 | children of the given process |
-t pts/0 | by terminal |
-N | invert the selection |
What to show
| Option | What it does |
|---|
-f | full format: UID PID PPID C STIME TTY TIME CMD |
-F | extra full: plus SZ RSS PSR |
u | BSD "user" format: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND |
-l | long format with F S PRI NI WCHAN |
-o field,field | custom columns; -o pid= — no header; -o user:20 — column width |
-O field | default columns plus the given ones |
--no-headers / h | no header line |
--sort=-%mem,pid | sorting; - before a field — descending |
--forest / f | process tree (indentation and \_) |
-H | hierarchy by indentation |
-L / -T | show threads (columns LWP/SPID, NLWP) |
ww / -ww | don't truncate long command lines |
Columns
Column (-o name) | Meaning |
|---|
PID (pid), PPID (ppid) | process ID and its parent's ID |
USER/UID (user, uid) | owner (effective) |
%CPU (%cpu) | CPU share averaged over the process's whole lifetime, not "right now" |
%MEM (%mem) | share of physical memory (by RSS) |
RSS (rss) | physical memory in use, KiB |
VSZ (vsz) | virtual memory, KiB (including what isn't loaded) |
TTY (tty) | terminal; ? — process without a terminal (a daemon) |
STAT (stat) | state, see below |
START/STIME (start, stime) | start time; lstart — full date |
ELAPSED (etime, etimes) | how long the process has lived: [[dd-]hh:]mm:ss / in seconds |
TIME (time) | total CPU time |
NI (ni), PRI (pri) | nice (−20…19) and priority |
CMD/COMMAND (cmd, args) | full command line; comm — name only (up to 15 characters) |
WCHAN (wchan) | kernel function the process is sleeping in |
NLWP (nlwp) | number of threads |
STAT codes
| Code | Meaning |
|---|
R | running or runnable |
S | sleeping, waiting for an event (the usual state) |
D | uninterruptible sleep — usually waiting for disk or network; signals, even KILL, have no effect until it leaves this state |
Z | zombie <defunct>: exited, but the parent hasn't collected the exit code yet |
T / t | stopped (SIGSTOP, Ctrl+Z) / stopped by a debugger |
I | idle kernel thread |
s | session leader |
< / N | high / low priority (nice) |
l | multithreaded |
+ | in the terminal's foreground process group |
Examples
# all processes, the two classic variants
ps aux
ps -ef
# custom columns, sorted by memory, top 10
ps -eo pid,ppid,user,%mem,rss,cmd --sort=-%mem | head -n 11
# processes named nginx
ps -C nginx -o pid,ppid,stat,cmd
# PID only, no header — for substitution in a script
ps -C cron -o pid=
# who is the parent of a process and when it started
ps -o ppid=,lstart= -p 1234
# process tree
ps -ef --forest
ps axf -o pid,args
# children of process 1
ps --ppid 1 -o pid,cmd
# zombies
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /^Z/'
# how many seconds a process has been alive
ps -o etimes= -p 1234
# grep the list without matching the grep line itself
ps aux | grep '[n]ginx'
Pitfalls
ps aux | grep name also finds grep itself. Use pgrep, ps -C name or the square-bracket trick: grep '[n]ginx'.
%CPU is an average over the process's lifetime. A process that has just started loading the CPU may show 0.5%. Look at current load in top.
-C searches by executable name, not by arguments: ps -C python3 finds Python scripts, but ps -C myscript.py doesn't. For arguments — pgrep -f.
comm is truncated to 15 characters, and the name can be spoofed. The full line is in args/cmd.
- A zombie can't be killed with
kill: it's already dead. It disappears when the parent calls wait or exits itself.
- The output is a snapshot. Between
ps and the next command a process may exit and its PID may go to another one.
- BSD and UNIX options differ:
ps -u root means root's processes, while ps u is an output format.
See also
pgrep · kill · proc · grep