Skip to main content
Bash and Linux

Read ps Output

mediumProcess management

Problem statement

Read a saved ps aux output and answer three questions: which processes use the most memory, how many processes each user runs, and are there any zombie processes. When a server is slow or out of memory, ps is where you look first, and reading its columns quickly is a core skill.

ps.txt (saved from ps aux on a server)

Bash
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 167000 11000 ? Ss 09:00 0:02 /sbin/init
postgres 812 1.2 12.5 2400000 1024000 ? Ss 09:01 3:10 postgres: main
app 1201 35.0 22.3 3500000 1830000 ? Sl 09:02 40:11 java -jar shop.jar
app 1290 0.5 4.1 900000 336000 ? S 09:02 0:40 node worker.js
www-data 1402 0.3 1.2 300000 98000 ? S 09:03 0:05 nginx: worker process
root 1550 0.0 0.0 0 0 ? Z 09:10 0:00 [cron] <defunct>
app 1610 2.0 6.8 1200000 557000 ? Sl 09:15 1:02 python3 report.py
root 1700 0.0 0.0 10000 3000 pts/0 R+ 09:20 0:00 ps aux
  1. Print the top 3 processes by memory: PID, %MEM, memory in MiB (RSS is in KiB) and the full command.
  2. Print how many processes each user runs, most first, ties in name order.
  3. Print any zombie processes.

Expected output:

Bash
== top 3 by memory ==
PID 1201 22.3% 1787 MiB java -jar shop.jar
PID 812 12.5% 1000 MiB postgres: main
PID 1610 6.8% 544 MiB python3 report.py
== processes per user ==
app 3
root 3
postgres 1
www-data 1
== zombie processes ==
PID 1550 [cron] <defunct>

Hints

Hint 1: Skip the header with NR > 1. %MEM is field 4 and RSS is field 6, so sort -k4,4nr sorts by memory, biggest first.

Approach

Optimal: awk and sort on ps columns

Covers: ps aux columns, RSS vs VSZ, STAT codes, NR > 1, sort -k4,4nr, joining fields 11 to NF, ps --sort, zombies.

What the columns mean.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB L["app 1201 35.0 22.3 3500000 1830000 ... Sl ... java -jar shop.jar"]:::gray L --> U["$1 USER
app"]:::blue L --> P["$2 PID
1201"]:::purple L --> MEM["$4 %MEM
22.3"]:::red L --> R["$6 RSS KiB
1830000"]:::yellow L --> S["$8 STAT
Sl"]:::green classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px
Column Field Means
USER 1 who runs it
PID 2 process ID, used with kill
%CPU 3 CPU share, averaged over its lifetime
%MEM 4 share of the machine's RAM it uses now
VSZ 5 virtual memory in KiB: everything it has mapped, much of it never used
RSS 6 resident memory in KiB: real RAM in use right now
STAT 8 state, see below
COMMAND 11+ the command line, which can contain spaces

RSS is the number that matters. VSZ looks scary for programs like Java, but it counts memory that is only reserved. RSS is what is really in RAM. Divide by 1024 for MiB. Shared libraries are counted in every process that uses them, so adding up RSS over-counts a little.

Process states. The first letter of STAT is the state, and extra letters add details:

Letter State
R running or ready to run
S sleeping, waiting for something (most processes)
D waiting on disk or network, cannot be interrupted
Z zombie: finished, but its parent has not collected its exit code
T stopped, for example with Ctrl+Z
s, l, + session leader, multi-threaded, in the foreground

Walking through the code. The # Setup: lines only save the sample output, so skip past them.

  1. NR > 1 skips the header, sort -k4,4nr sorts by %MEM, and head -n 3 keeps the top three. The awk prints PID, %MEM and RSS / 1024, then joins fields 11 to NF back into the full command.
  2. awk 'NR > 1 {print $1}' | sort | uniq -c counts processes per user, and sort -k1,1nr -k2,2 ranks them with ties in name order.
  3. $8 ~ /Z/ keeps lines whose STAT contains Z.

Edge cases. ps cuts long command lines to the screen width; use ps auxww for the full line. On a live server, ps aux --sort=-%mem | head (GNU) sorts for you, and top then M sorts by memory interactively.

# Setup: save the sample ps aux output in a fresh temporary folder
cd "$(mktemp -d)"
cat > ps.txt << 'PS'
USER         PID %CPU %MEM     VSZ     RSS TTY   STAT START   TIME COMMAND
root           1  0.0  0.1  167000   11000 ?     Ss   09:00   0:02 /sbin/init
postgres     812  1.2 12.5 2400000 1024000 ?     Ss   09:01   3:10 postgres: main
app         1201 35.0 22.3 3500000 1830000 ?     Sl   09:02  40:11 java -jar shop.jar
app         1290  0.5  4.1  900000  336000 ?     S    09:02   0:40 node worker.js
www-data    1402  0.3  1.2  300000   98000 ?     S    09:03   0:05 nginx: worker process
root        1550  0.0  0.0       0       0 ?     Z    09:10   0:00 [cron] <defunct>
app         1610  2.0  6.8 1200000  557000 ?     Sl   09:15   1:02 python3 report.py
root        1700  0.0  0.0   10000    3000 pts/0 R+   09:20   0:00 ps aux
PS

echo "== top 3 by memory =="
awk 'NR > 1' ps.txt | sort -k4,4nr | head -n 3 |
  awk '{ cmd = $11; for (i = 12; i <= NF; i++) cmd = cmd " " $i
         printf "PID %s  %s%%  %.0f MiB  %s\n", $2, $4, $6 / 1024, cmd }'

echo "== processes per user =="
awk 'NR > 1 { print $1 }' ps.txt | sort | uniq -c | sort -k1,1nr -k2,2 | awk '{ print $2, $1 }'

echo "== zombie processes =="
awk 'NR > 1 && $8 ~ /Z/ { print "PID", $2, $11, $12 }' ps.txt

Interview follow-ups

  • Watch whether the java process's memory keeps growing.

    Sample its RSS every few seconds and log it: while sleep 10; do echo "$(date +%T) $(ps -o rss= -p 1201)"; done >> rss.log. -o rss= prints only the number, with no header. A value that climbs steadily and never drops after garbage collection suggests a memory leak. On a real system, a metrics agent does this for you, but this loop works anywhere in a pinch.

Frequently asked questions

You cannot kill a zombie, because it has already finished; it is only a leftover entry holding its exit code. The parent process is supposed to collect it with wait. Find the parent with ps -o ppid= -p <zombie_pid>, then fix or restart that parent. If the parent dies, the zombie is adopted by PID 1, which collects it. A few zombies are harmless; thousands mean a buggy parent and can use up process IDs.

ps shows CPU time divided by how long the process has existed, so a process that was busy an hour ago and idle now still shows a high number. top shows usage over the last refresh interval, usually a few seconds, which is what you want for "what is busy right now". For a quick live view without top, run ps -eo pid,pcpu,comm --sort=-pcpu | head twice a few seconds apart, or use pidstat 1 if it is installed.

ps aux (BSD style) and ps -ef (UNIX style) both list every process; -ef also shows the parent PID. ps -eo pid,user,rss,comm lets you pick exactly the columns you want, which is best for scripts. ps -p 1201 -o pid,etime,cmd shows one process and how long it has run. ps --forest or pstree show who started whom.