Processes
What a process is, how PIDs, parents and the process tree work, how fork and exec start every command, process states (R, S, D, T, Z), reading ps aux and ps -ef column by column, sorting and custom columns, pgrep, pidof and pstree, looking inside a process through /proc, and zombies and orphans.
Programs and processes
A program is a file: /usr/bin/nginx, /usr/bin/python3, a script you wrote. A process is a program running: the kernel has loaded it into memory, given it an ID, and is scheduling it on the CPU.
You can run the same program many times. Each copy is a separate process with:
- its own PID (process ID),
- its own memory,
- its own open files and network connections,
- an identity (a user and groups) that decides what it may access,
- a parent, the process that started it.
As a DevOps engineer you'll look at processes constantly: "is the app running?", "what's using all the CPU?", "why won't this port free up?", "what is this server even doing?". This lesson is about seeing them. The next lesson is about controlling them.
One program, many processes
Each process has its own PID, memory, open files and user, even though they run the same program.
The process tree
Every process is started by another one, so processes form a tree:
- PID 1 is the first process the kernel starts. On real servers it's systemd, which starts all services. If PID 1 dies, the system goes down.
- Your SSH login starts a shell (bash).
- Every command you type is a child of that shell.
Starting a process is a two-step dance called fork and exec: the parent makes a copy of itself (fork), and the copy replaces itself with the new program (exec). That's why a child inherits so much from its parent: its user, its environment variables, its working directory, its umask, and its open files.
fork, then exec
pstree -p-p shows PIDs; -u marks user changes; pstree -p 34 shows one subtree.
shack-init(1)-+-batch-runner(32)---batch-job(38)
|-report-gen(30)
|-sleep(7)
`-su(31)---shop-supervisor(34)-+-shop-worker(35)
|-shop-worker(36)
`-shop-worker(37)On the lab machines, PID 1 is a small init script; on a full server it's systemd with dozens of services below it. Here you can read that su started shop-supervisor, which started three workers.
Process states
A process isn't always running. Most of the time it's waiting for something. The STAT column in ps shows its state:
| State | Name | Meaning |
|---|---|---|
R | running | on a CPU right now, or ready and waiting for one |
S | sleeping | waiting for an event (a network request, a timer, input). Most processes, most of the time |
D | uninterruptible sleep | stuck inside the kernel waiting for I/O, usually disk or NFS. Can't be killed until the I/O returns |
T | stopped | paused (Ctrl+Z, or a debugger) |
Z | zombie | finished, but its parent hasn't collected its exit status yet |
Extra letters add detail: s session leader, + in the foreground of a terminal, l multi-threaded, < high priority, N low priority.
A server with a lot of D processes usually has a storage problem, not a CPU problem: that's one of the most useful things to know when someone says "the server is slow".
ps: a snapshot of processes
ps prints the processes at one moment. Two styles are in common use, and you'll see both in documentation:
ps aux # BSD style: a=all users, u=user columns, x=include no-terminal processes
ps -ef # System V style: -e=every process, -f=full format (shows PPID)USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.3 0.0 4760 3748 ? Ss 16:08 0:00 /bin/bash /usr/local/bin/shack-in
root 7 0.0 0.0 3132 2032 ? S 16:08 0:00 sleep 3600
root 30 67.3 0.1 15688 10632 ? Ss 16:08 0:03 python3 /opt/reports/generator.py
root 31 0.0 0.0 4768 3252 ? Ss 16:08 0:00 su -s /bin/bash appsvc -c exec py
root 32 0.0 0.1 15556 10008 ? Ss 16:08 0:00 python3 /opt/shop/batch.py| Column | Meaning |
|---|---|
USER | the account the process runs as |
PID | process ID |
%CPU | CPU use (percent of one core, averaged over its lifetime) |
%MEM | percent of total RAM |
VSZ | virtual memory size in KB: everything it has reserved, much of it never used |
RSS | resident set size in KB: real RAM in use right now. This is the memory number that matters |
TTY | controlling terminal; ? for services and background daemons |
STAT | state (table above) |
START | when it started |
TIME | total CPU time used so far |
COMMAND | the full command line |
Sorting and choosing columns
ps aux --sort=-%cpu | head -5 # top CPU users
ps aux --sort=-rss | head -5 # top memory users
ps -eo pid,ppid,user,%cpu,%mem,rss,etime,comm --sort=-%cpu | head -6| Option | Meaning |
|---|---|
--sort=-%cpu | sort by CPU, descending (+ or nothing for ascending) |
-o col,col,... | output only these columns |
etime | elapsed time since the process started |
comm | short process name (vs cmd/args for the full command line) |
PID USER %CPU %MEM RSS ELAPSED COMMAND
30 root 66.2 0.1 10632 00:05 report-gen
1 root 0.3 0.0 3748 00:06 shack-init
34 appsvc 0.1 0.1 9992 00:05 shop-supervisor
7 root 0.0 0.0 2032 00:06 sleepThat's the -eo version (with fewer columns). Custom columns are how you build exactly the view you need, and how scripts get clean output to parse.
Filtering
ps -u appsvc # processes of one user
ps -p 30,34 -o pid,ppid,cmd # specific PIDs
ps --ppid 34 # children of PID 34
ps aux | grep [n]ginx # by name; the [n] trick stops grep matching itselfFind the CPU hog
Find the process using the most CPU and record its PID.
ps (process status) takes a snapshot of the processes running right now. Plain ps shows only processes attached to your terminal, so on servers you almost always use ps aux (BSD style) or ps -ef (System V style): every process on the machine.
1. Every process
ps aux| Letter | Meaning |
|---|---|
a | processes of all users |
u | user-oriented columns (USER, %CPU, %MEM...) |
x | include processes with no terminal (services, background jobs) |
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.3 0.0 4760 3748 ? Ss 16:08 0:00 /bin/bash /usr/local/bin/shack-in
root 7 0.0 0.0 3132 2032 ? S 16:08 0:00 sleep 3600
root 30 67.3 0.1 15688 10632 ? Ss 16:08 0:03 python3 /opt/reports/generator.py
root 31 0.0 0.0 4768 3252 ? Ss 16:08 0:00 su -s /bin/bash appsvc -c exec py
root 32 0.0 0.1 15556 10008 ? Ss 16:08 0:00 python3 /opt/shop/batch.py(Your PIDs will differ, and you'll also see your own bash and the ps command itself.)
| Column | Meaning |
|---|---|
%CPU | share of one CPU core used, averaged over the process's life |
%MEM | share of the machine's RAM in use |
VSZ / RSS | virtual memory reserved / real memory actually used (KB) |
TTY | the terminal it's attached to; ? = none (a background service) |
STAT | state: R running, S sleeping, D waiting on disk, T stopped, Z zombie; s = session leader |
TIME | total CPU time consumed |
PID 30 stands out: 67% of a CPU core.
2. Sort by CPU
ps aux --sort=-%cpu | head -5--sort=-%cpu sorts by CPU, highest first (the - means descending). --sort=-%mem sorts by memory.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 30 67.3 0.1 15688 10632 ? Ss 16:08 0:03 python3 /opt/reports/generator.py
root 1 0.3 0.0 4760 3748 ? Ss 16:08 0:00 /bin/bash /usr/local/bin/shack-in3. Choose your own columns, and search by name
ps -eo pid,user,%cpu,%mem,etime,comm --sort=-%cpu | head -4
pgrep report-gen| Part | Meaning |
|---|---|
-e | every process |
-o pid,user,... | only these columns; etime = time since start, comm = process name |
pgrep NAME | print the PIDs of processes with that name |
PID USER %CPU %MEM ELAPSED COMMAND
30 root 66.2 0.1 00:05 report-gen
1 root 0.3 0.0 00:06 shack-init
34 appsvc 0.1 0.1 00:05 shop-supervisor
30comm shows the short process name (report-gen), while ps aux shows the full command line (python3 /opt/reports/generator.py). pgrep matches the name; pgrep -f matches the full command line.
Save the PID of the process using the most CPU (just the number) to /root/hog.pid.
Expected result: cat /root/hog.pid prints the PID of report-gen.
Hint: the commands, explained
ps aux --sort=-%cpu | head -3
pgrep report-gen > /root/hog.pidWhat each part does
ps aux: every process with its CPU and memory use.--sort=-%cpu: sort by CPU, highest first (the-means descending).head -3: the header plus the top 2.pgrep report-gen: print the PID of the process with that name.>saves it.
pgrep, pidof and pkill
Finding PIDs by name is so common it has its own tools:
pgrep report-gen # PIDs whose name matches
pgrep -l shop # with names
pgrep -a -u appsvc # a user's processes, with full command lines
pgrep -f generator.py # match against the full command line
pidof sshd # exact program name, all PIDs on one line30
34 shop-supervisor
35 shop-worker
36 shop-worker
37 shop-workerThe first two commands above. pgrep matches a pattern, so pgrep shop found the supervisor and all workers. Use pgrep -x name for an exact match. Its partner, pkill, sends signals by name (next lesson), so always check with pgrep first what a pattern matches.
Parents and children
Find out which process started the three shop workers.
Every process (except PID 1) was started by another process, its parent. Its PPID column holds the parent's PID. Following PPIDs answers questions like "what started these workers?" and "if I stop this, what else goes away?".
1. Find the workers
pgrep -l shop-l also prints the name next to each PID.
34 shop-supervisor
35 shop-worker
36 shop-worker
37 shop-worker2. See their parents
ps -o pid,ppid,user,stat,etime,cmd -p $(pgrep -d, shop)| Part | Meaning |
|---|---|
-p 34,35,... | only these PIDs |
pgrep -d, | print the PIDs separated by commas, exactly what -p wants |
$( ... ) | insert the output of a command (command substitution) |
PID PPID USER STAT ELAPSED CMD
34 31 appsvc Ss 00:05 python3 /opt/shop/supervisor.py
35 34 appsvc S 00:05 python3 /opt/shop/supervisor.py
36 34 appsvc S 00:05 python3 /opt/shop/supervisor.py
37 34 appsvc S 00:05 python3 /opt/shop/supervisor.pyAll three workers have PPID 34: the supervisor started them. They run as appsvc, not root. And the supervisor's own parent is 31.
3. The whole tree at once
pstree -p-p shows PIDs. Add -u to show where the user changes.
shack-init(1)-+-batch-runner(32)---batch-job(38)
|-report-gen(30)
|-sleep(7)
`-su(31)---shop-supervisor(34)-+-shop-worker(35)
|-shop-worker(36)
`-shop-worker(37)PID 1 at the root (on a full server it's systemd; this lab machine uses a small init script). su (31) switched to appsvc and started the supervisor, which started the workers.
Save the PID of the process that started the three shop workers to /root/supervisor.pid.
Expected result: the file contains the PID of shop-supervisor, the PPID of every shop-worker.
Hint: the commands, explained
ps -o pid,ppid,comm -p $(pgrep -d, shop-worker)The PPID column holds the answer. Or: pgrep shop-supervisor > /root/supervisor.pid
What each part does
pgrep -d, shop-worker: all PIDs with that name, joined by commas (-d,), like41,42,43.ps -o pid,ppid,comm -p LIST: for just those PIDs, show the PID, the parent PID and the name. Every worker shows the same PPID: that's the supervisor.
Inside a process: /proc
/proc is a virtual filesystem: nothing is stored on disk. The kernel shows each process as a directory named after its PID, with live information inside:
P=$(pgrep report-gen)
ls -l /proc/$P/cwd /proc/$P/exe
grep -E '^(Name|State|PPid|Uid|VmRSS|Threads)' /proc/$P/status
ls -l /proc/$P/fdlrwxrwxrwx 1 root root 0 Nov 24 16:08 /proc/30/cwd -> /opt/reports
lrwxrwxrwx 1 root root 0 Nov 24 16:08 /proc/30/exe -> /usr/bin/python3.12
Name: report-gen
State: S (sleeping)
PPid: 1
Uid: 0 0 0 0
VmRSS: 10632 kB
Threads: 1
total 0
lr-x------ 1 root root 64 Nov 24 16:08 0 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 1 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 2 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 3 -> /var/log/report-gen.log| Path | What it tells you |
|---|---|
/proc/PID/cmdline | exact command line (null-separated: tr '\0' ' ' < to read it) |
/proc/PID/cwd | working directory |
/proc/PID/exe | the program file running (shows (deleted) if it was replaced on disk, a sign the service needs a restart after an upgrade) |
/proc/PID/environ | environment variables it started with (root only) |
/proc/PID/status | name, state, parent, user, memory, threads |
/proc/PID/fd/ | every open file, socket and pipe |
/proc/PID/limits | resource limits (open files, processes) |
The file descriptors 0, 1 and 2 are standard input, output and error, the same streams you redirect with <, > and 2>. Everything numbered 3 and up is something the process opened itself.
lsof -p PID gives the same information in one table, and lsof /var/log/report-gen.log answers the reverse question: which process has this file open?
Inside a process with /proc
Find which file the report generator is writing to, without reading its code.
/proc isn't a real folder on disk. The kernel generates it on the fly, with one directory per PID (/proc/30, /proc/34...) showing that process's live details: its command line, working directory, memory, limits and every file it has open. Tools like ps, top and lsof simply read /proc.
1. Where is it running from?
P=$(pgrep report-gen)
ls -l /proc/$P/cwd /proc/$P/exe
tr '\0' ' ' < /proc/$P/cmdline; echo| Entry | Shows |
|---|---|
cwd | the process's current working directory |
exe | the program file actually running |
cmdline | the full command line (arguments separated by null bytes, so tr turns them into spaces) |
lrwxrwxrwx 1 root root 0 Nov 24 16:08 /proc/30/cwd -> /opt/reports
lrwxrwxrwx 1 root root 0 Nov 24 16:08 /proc/30/exe -> /usr/bin/python3.12
python3 /opt/reports/generator.py 2. Status in plain text
grep -E '^(Name|State|PPid|Uid|VmRSS|Threads)' /proc/$P/statusName: report-gen
State: S (sleeping)
PPid: 1
Uid: 0 0 0 0
VmRSS: 10632 kB
Threads: 1State: S: at this exact moment it's in its short sleep between bursts of work. Uid: 0 means root. VmRSS is its real memory use.
3. Its open files
ls -l /proc/$P/fdfd = file descriptors: every file, socket and pipe the process has open, by number.
total 0
lr-x------ 1 root root 64 Nov 24 16:08 0 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 1 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 2 -> /dev/null
l-wx------ 1 root root 64 Nov 24 16:08 3 -> /var/log/report-gen.log| fd | Meaning |
|---|---|
0, 1, 2 | standard input, output and error (here thrown away to /dev/null) |
3 | a file it opened itself, for writing (l-wx): its log |
lsof -p $P shows the same and more (sockets, libraries). This is how you find what a process is writing to, which disk it's filling, or which deleted file still takes up space.
Find the file report-gen has open for writing (other than /dev/null) and save its full path to /root/hog-log.txt.
Expected result: /root/hog-log.txt contains the path of the generator's log file.
Hint: the commands, explained
ls -l /proc/$(pgrep report-gen)/fd
echo /var/log/report-gen.log > /root/hog-log.txtWhat each part does
/proc/PID/fd: a folder holding one entry per file the process has open (fd = file descriptor).ls -lshows each as a link:3 -> /var/log/report-gen.log. That is the file it's writing to.- Save that path into the answer file.
Zombies and orphans
When a process exits, the kernel keeps a tiny record of it (its PID and exit code) until the parent reads it with wait(). Between exiting and being collected, the process is a zombie:
ps -eo pid,ppid,stat,comm | awk 'NR==1 || $3 ~ /Z/' PID PPID STAT COMMAND
38 32 Z batch-jobbatch-job has finished, but its parent (32, batch-runner) never called wait().
- A zombie uses no CPU or memory, only a process-table slot.
- You can't kill a zombie; it's already dead.
- The fix is the parent: fix its code, or restart it. When a parent exits, its children are adopted by PID 1 (or a "subreaper" like systemd), which collects zombies immediately.
An orphan is the opposite case: a process whose parent exited while it kept running. Orphans are normal: PID 1 adopts them, which is why many long-running processes show PPID 1. That's exactly how background daemons are started.
Trusting VSZ as memory use. VSZ includes memory that was reserved but never touched. Look at RSS.
ps aux | grep nginx and counting the grep. The grep process matches itself. Use pgrep nginx or grep [n]ginx.
Reading %CPU in ps as "right now". It's averaged over the process's whole life. For live usage use top (in Monitoring CPU, Memory and Load).
Trying to kill -9 a zombie or a D process. Neither can react. Fix the parent, or the storage the D process is waiting on.
"What's the difference between a process and a thread?" A process has its own memory space; threads are execution paths inside one process that share its memory. ps -eLf or the Threads line in /proc/PID/status shows them.
"What is PID 1?" The first user-space process, started by the kernel, usually systemd. It starts services and adopts orphaned processes.
"What is a zombie process and how do you get rid of it?" A finished process whose parent hasn't read its exit status. Fix or restart the parent.
"How do you find which process is using the most memory?" ps aux --sort=-rss | head, or top sorted by memory.
"How do you find what files a process has open?" ls -l /proc/PID/fd or lsof -p PID.
The zombie
Find the zombie process and the parent responsible for it.
When a process finishes, it doesn't disappear completely. It stays in the process table as a zombie (state Z, shown as <defunct>) until its parent reads its exit status with wait(). Normally that happens instantly. A parent that never does it leaves zombies behind.
A zombie uses no CPU and no memory, only a slot in the process table, and it can't be killed: it's already dead. The fix is always the parent: make it collect its children, or stop it, after which PID 1 adopts the zombie and cleans it up.
1. Find zombies
ps -eo pid,ppid,stat,comm | awk 'NR==1 || $3 ~ /Z/'awk prints the header line (NR==1) and every line whose third column (STAT) contains Z.
PID PPID STAT COMMAND
38 32 Z batch-jobbatch-job (38) has finished, but its parent, 32, never collected it.
2. Look at the parent
ps -o pid,ppid,stat,etime,comm -p 32 PID PPID STAT ELAPSED COMMAND
32 1 Ss 00:05 batch-runnerbatch-runner is alive and sleeping, and has simply never called wait(). One zombie is harmless. A buggy parent creating thousands can fill the process table so no new process can start, which is when you restart the parent.
top shows the number of zombies in its header line (0 zombie). A non-zero number that keeps growing points at a buggy parent.
Save the PID of the zombie's parent to /root/zombie-parent.pid. Don't kill anything yet.
Expected result: the file contains the PID of batch-runner.
Hint: the commands, explained
ps -eo pid,ppid,stat,comm | awk '$3 ~ /Z/'The second column (PPID) is the parent.
What each part does
ps -eo pid,ppid,stat,comm: every process (-e) with the chosen columns, including STAT, its state.awk '$3 ~ /Z/': keep lines where column 3 containsZ, which means zombie. The PPID column shows the parent that didn't clean it up.
Summary
- A process is a running program with a PID, a parent (PPID), a user, memory and open files.
- Processes form a tree under PID 1 (systemd); children are created by fork + exec and inherit user, environment and cwd.
- States:
Rrunning,Ssleeping,Dwaiting on I/O,Tstopped,Zzombie. ps aux/ps -ef;--sort=-%cpuor-rss;-eofor custom columns. RSS is the real memory number.pgrep,pidof,pstree -pfind processes;/proc/PID/(andlsof) look inside them.- Zombies are fixed through their parent; orphans are adopted by PID 1.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.