Learn / Linux / Processes & System
Lesson 23 of 74

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.

25 min read Report a problem Hands-on lab included 6-question quiz

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

/usr/bin/python3one file on disk
started 3 times
PID 30 · report-genroot · writes /var/log/report-gen.log
PID 32 · batch-runnerroot · sleeping
PID 34 · shop-supervisorappsvc · parent of 3 workers

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

bash (PID 39)you type: ls
fork()
bash copy (PID 50)same user, env, cwd
exec("ls")
ls (PID 50)new program, same PID
exit
bashwait() collects the exit code
bash
pstree -p

-p shows PIDs; -u marks user changes; pstree -p 34 shows one subtree.

text
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:

StateNameMeaning
Rrunningon a CPU right now, or ready and waiting for one
Ssleepingwaiting for an event (a network request, a timer, input). Most processes, most of the time
Duninterruptible sleepstuck inside the kernel waiting for I/O, usually disk or NFS. Can't be killed until the I/O returns
Tstoppedpaused (Ctrl+Z, or a debugger)
Zzombiefinished, 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:

bash
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)
text
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
ColumnMeaning
USERthe account the process runs as
PIDprocess ID
%CPUCPU use (percent of one core, averaged over its lifetime)
%MEMpercent of total RAM
VSZvirtual memory size in KB: everything it has reserved, much of it never used
RSSresident set size in KB: real RAM in use right now. This is the memory number that matters
TTYcontrolling terminal; ? for services and background daemons
STATstate (table above)
STARTwhen it started
TIMEtotal CPU time used so far
COMMANDthe full command line

Sorting and choosing columns

bash
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
OptionMeaning
--sort=-%cpusort by CPU, descending (+ or nothing for ascending)
-o col,col,...output only these columns
etimeelapsed time since the process started
commshort process name (vs cmd/args for the full command line)
text
    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 sleep

That'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

bash
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 itself
Try it · Task 1 of 4

Find the CPU hog

Find the process using the most CPU and record its PID.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ps aux
LetterMeaning
aprocesses of all users
uuser-oriented columns (USER, %CPU, %MEM...)
xinclude processes with no terminal (services, background jobs)
text
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.)

ColumnMeaning
%CPUshare of one CPU core used, averaged over the process's life
%MEMshare of the machine's RAM in use
VSZ / RSSvirtual memory reserved / real memory actually used (KB)
TTYthe terminal it's attached to; ? = none (a background service)
STATstate: R running, S sleeping, D waiting on disk, T stopped, Z zombie; s = session leader
TIMEtotal CPU time consumed

PID 30 stands out: 67% of a CPU core.

2. Sort by CPU

bash
ps aux --sort=-%cpu | head -5

--sort=-%cpu sorts by CPU, highest first (the - means descending). --sort=-%mem sorts by memory.

text
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-in

3. Choose your own columns, and search by name

bash
ps -eo pid,user,%cpu,%mem,etime,comm --sort=-%cpu | head -4
pgrep report-gen
PartMeaning
-eevery process
-o pid,user,...only these columns; etime = time since start, comm = process name
pgrep NAMEprint the PIDs of processes with that name
text
    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
30

comm 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.

2 · Your task

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
text
ps aux --sort=-%cpu | head -3
pgrep report-gen > /root/hog.pid

What 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.
First start the lab machine in the terminal panel on the right.

pgrep, pidof and pkill

Finding PIDs by name is so common it has its own tools:

bash
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 line
text
30
34 shop-supervisor
35 shop-worker
36 shop-worker
37 shop-worker

The 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.

Try it · Task 2 of 4

Parents and children

Find out which process started the three shop workers.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
pgrep -l shop

-l also prints the name next to each PID.

text
34 shop-supervisor
35 shop-worker
36 shop-worker
37 shop-worker

2. See their parents

bash
ps -o pid,ppid,user,stat,etime,cmd -p $(pgrep -d, shop)
PartMeaning
-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)
text
    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.py

All 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

bash
pstree -p

-p shows PIDs. Add -u to show where the user changes.

text
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.

2 · Your task

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
text
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,), like 41,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.
First start the lab machine in the terminal panel on the right.

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:

bash
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/fd
text
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
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
PathWhat it tells you
/proc/PID/cmdlineexact command line (null-separated: tr '\0' ' ' < to read it)
/proc/PID/cwdworking directory
/proc/PID/exethe program file running (shows (deleted) if it was replaced on disk, a sign the service needs a restart after an upgrade)
/proc/PID/environenvironment variables it started with (root only)
/proc/PID/statusname, state, parent, user, memory, threads
/proc/PID/fd/every open file, socket and pipe
/proc/PID/limitsresource 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?

Try it · Task 3 of 4

Inside a process with /proc

Find which file the report generator is writing to, without reading its code.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

/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?

bash
P=$(pgrep report-gen)
ls -l /proc/$P/cwd /proc/$P/exe
tr '\0' ' ' < /proc/$P/cmdline; echo
EntryShows
cwdthe process's current working directory
exethe program file actually running
cmdlinethe full command line (arguments separated by null bytes, so tr turns them into spaces)
text
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

bash
grep -E '^(Name|State|PPid|Uid|VmRSS|Threads)' /proc/$P/status
text
Name:	report-gen
State:	S (sleeping)
PPid:	1
Uid:	0	0	0	0
VmRSS:	   10632 kB
Threads:	1

State: 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

bash
ls -l /proc/$P/fd

fd = file descriptors: every file, socket and pipe the process has open, by number.

text
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
fdMeaning
0, 1, 2standard input, output and error (here thrown away to /dev/null)
3a 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.

2 · Your task

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
text
ls -l /proc/$(pgrep report-gen)/fd
echo /var/log/report-gen.log > /root/hog-log.txt

What each part does

  • /proc/PID/fd: a folder holding one entry per file the process has open (fd = file descriptor).
  • ls -l shows 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.
First start the lab machine in the terminal panel on the right.

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:

bash
ps -eo pid,ppid,stat,comm | awk 'NR==1 || $3 ~ /Z/'
text
    PID    PPID STAT COMMAND
     38      32 Z    batch-job

batch-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.

Try it · Task 4 of 4

The zombie

Find the zombie process and the parent responsible for it.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
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.

text
    PID    PPID STAT COMMAND
     38      32 Z    batch-job

batch-job (38) has finished, but its parent, 32, never collected it.

2. Look at the parent

bash
ps -o pid,ppid,stat,etime,comm -p 32
text
    PID    PPID STAT     ELAPSED COMMAND
     32       1 Ss         00:05 batch-runner

batch-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.

2 · Your task

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
text
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 contains Z, which means zombie. The PPID column shows the parent that didn't clean it up.
First start the lab machine in the terminal panel on the right.

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: R running, S sleeping, D waiting on I/O, T stopped, Z zombie.
  • ps aux / ps -ef; --sort=-%cpu or -rss; -eo for custom columns. RSS is the real memory number.
  • pgrep, pidof, pstree -p find processes; /proc/PID/ (and lsof) look inside them.
  • Zombies are fixed through their parent; orphans are adopted by PID 1.
Check your understanding

Quick quiz

Feedback