Where to start First steps track · 15 stages
Linux Labs

ps — list processes

5 min Ubuntu 24.04 · procps-ng 4.0.4

The ps command lists running processes with PID, owner, state, CPU and memory use, and the command line.

Cheat sheet

10 commands Printable version
  • ps processes of this terminal
  • ps aux all processes, BSD style
  • ps -ef all processes, with the parent PPID
  • ps aux --sort=-%cpu | head most CPU
  • ps aux --sort=-%mem | head most memory
  • ps -u student a user's processes
  • ps -p 1234 -o pid,stat,etime,cmd custom columns for a PID
  • ps -C nginx by program name
  • ps -ef --forest a tree: who started whom
  • pgrep -a nginx PID and command line by name

Overview

A process is a running program. Each has a PID number, an owner, a parent (PPID) and a state. ps (process status) prints a snapshot of processes at the moment it runs; top keeps a live view.

With no options ps shows only the current terminal's processes:

text
student@ubuntu:~$ ps
    PID TTY          TIME CMD
    812 ttyS0    00:00:00 bash
   1377 ttyS0    00:00:00 ps

Usually you want all processes. For historical reasons ps has two syntaxes, and both work: ps aux (BSD, no dash) and ps -ef (UNIX, with a dash).

text
student@ubuntu:~$ ps -ef | head -3
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 09:02 ?        00:00:01 /sbin/init
root           2       0  0 09:02 ?        00:00:00 [kthreadd]

Columns: PPID — the parent's PID; STIME — start time; TTY — the terminal (? means none, usually a service); TIME — processor time used so far; CMD — the command. Names in square brackets are kernel threads.

Instead of PPID, ps aux shows %CPU and %MEM — share of processor and memory; VSZ and RSS — virtual and actually used memory in KiB; and STAT — the state.

Options

9

Options with a dash (-e, -f, -u) and without (a, u, x) are different sets, and they can be mixed. Full list — man ps.

Option What it does Example
aux All processes of all users, with owner, CPU and memory (BSD syntax). ps aux
-ef All processes in full format, with the parent PPID (UNIX syntax). ps -ef
-u user Processes of the given user. ps -u student
-p PID Only processes with these PIDs (comma-separated). ps -p 1,812
-C name Processes by program name. ps -C nginx
-o fields A custom set of columns — pid, ppid, user, stat, %cpu, %mem, etime, cmd. ps -o pid,ppid,stat,cmd -p 1
--sort field Sort by a column; a minus sign sorts descending. ps aux --sort=-%mem
--forest Draw the parent–child tree. ps -ef --forest
ww Don't cut the command line to the window width. ps auxww

Nothing found. Try a shorter query.

Recipes

4

What the STAT letters mean

Letter State
R running or waiting for a CPU
S sleeping, waiting for an event
D waiting on disk or network, signals can't interrupt
T stopped (Ctrl+Z or a STOP signal)
Z zombie: exited, the parent hasn't collected it
s, +, l session leader, foreground, multithreaded

Find a process by name without grep itself

bash
pgrep -a nginx                 # 1021 nginx: master process ...
ps aux | grep '[n]ginx'        # [n] doesn't match grep's own line

ps aux | grep nginx also finds the line of grep nginx itself — hence the "extra" process in the output.

Custom columns and the parent

text
student@ubuntu:~$ ps -o pid,ppid,user,stat,etime,cmd -p 1
    PID    PPID USER     STAT     ELAPSED CMD
      1       0 root     Ss         18:42 /sbin/init

etime is how long the process has run, ppid is the parent's PID: use it to find who started a process or who isn't reaping a zombie.

A service's process tree

bash
ps -ef --forest | grep -A3 '[n]ginx: master'
systemctl status nginx         # the same tree in the CGroup section

Troubleshooting

4

A process eats CPU, but ps shows little %CPU

%CPU in ps is the average over the process's lifetime, not "right now". top shows the current load (P sorts by CPU).

error: user name does not exist

ps -u expects an existing name or UID. Check the name: id name.

The process is listed, but kill doesn't help

State Z — the process has already exited and can't be killed: wait for the parent to collect it, or end the parent. State D — the process waits on I/O, and the signal takes effect when the operation finishes. More in kill and processes.

The command line is cut off

ps fits output to the window width. For the full line: ps auxww or ps -o args= -p PID.

See also

  • pgrep — find a PID by name or command line.
  • processes — what processes, signals and priorities are.
  • top — processes in real time.
  • kill — stop a process by PID.
  • system-monitoring — "the server is slow": CPU, memory, disk step by step.
  • background-jobs — terminal jobs, jobs, fg, bg.
  • lsof — which files and ports a process holds.
Loading…