Lesson 14: Process Management

Goal: Learn to monitor, control, and manage running processes and system resources.


Table of Contents

  1. List Processes — ps
  2. Live Monitor — top
  3. Interactive Monitor — htop & btop
  4. Kill Processes — kill & signals
  5. Background & Foreground Jobs
  6. Measure Execution Time — time
  7. Exercises

List Processes — ps

Show your processes

ps

Show ALL processes (most common)

ps aux

Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 169936 13212 ?        Ss   Feb05   0:12 /sbin/init
benjamin  1234  0.5  1.2 456780 98304 ?        Sl   10:00   0:45 /usr/bin/code
Column Meaning
USER Process owner
PID Process ID
%CPU CPU usage
%MEM Memory usage
VSZ Virtual memory (KB)
RSS Physical memory (KB)
TTY Terminal
STAT Status
START Start time
TIME Total CPU time
COMMAND Command that started the process

Process states (STAT)

Code State
R Running
S Sleeping (waiting for event)
D Uninterruptible sleep (usually I/O)
Z Zombie (terminated but not cleaned up)
T Stopped

Filter processes

# Find a specific process
ps aux | grep nginx

# Exclude the grep itself
ps aux | grep [n]ginx

# Show process tree
ps auxf

# Show processes for a specific user
ps -u benjamin

# Show by PID
ps -p 1234

# Custom output format
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -20

pgrep — find PIDs by name

pgrep nginx
pgrep -l nginx        # with process name
pgrep -a nginx        # with full command
pgrep -u benjamin     # by user

Live Monitor — top

top

top interface

top - 14:30:00 up 45 days,  3:22,  2 users,  load average: 0.12, 0.08, 0.05
Tasks: 245 total,   1 running, 244 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.5 us,  1.0 sy,  0.0 ni, 96.0 id,  0.5 wa,  0.0 hi,  0.0 si
MiB Mem:  16384.0 total,   8192.0 free,   4096.0 used,   4096.0 buff/cache
MiB Swap:  4096.0 total,   4096.0 free,      0.0 used.  11264.0 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM   TIME+ COMMAND
 1234 benjamin  20   0  456780  98304  45678 S   5.0   0.6   0:45 code

top keyboard shortcuts

Key Action
q Quit
h Help
M Sort by memory
P Sort by CPU
T Sort by time
k Kill a process (enter PID)
r Renice a process
1 Show individual CPUs
c Show full command
f Select fields to display
Space Refresh immediately

Interactive Monitor — htop & btop

htop

sudo apt install htop
htop

htop is a much better version of top with:

  • Color-coded output
  • Mouse support
  • Visual CPU/memory bars
  • Easy process killing with F9
  • Tree view with F5

htop keyboard shortcuts

Key Action
F1 Help
F2 Setup
F3 Search
F4 Filter
F5 Tree view
F6 Sort by...
F9 Kill process
F10 Quit
u Filter by user
t Toggle tree view

btop

sudo apt install btop
btop

btop is even more advanced with GPU monitoring, network graphs, and a modern interface.


Kill Processes — kill & signals

Basic kill

# Graceful shutdown (SIGTERM = 15)
kill 1234

# Force kill (SIGKILL = 9)
kill -9 1234

# Kill by name
killall nginx

# Kill by name (more flexible)
pkill nginx

# Kill by partial name match
pkill -f "python script.py"

Common signals

Signal Number Meaning
SIGHUP 1 Hang up (reload config)
SIGINT 2 Interrupt (Ctrl+C)
SIGQUIT 3 Quit with core dump
SIGKILL 9 Force kill (cannot be caught)
SIGTERM 15 Graceful termination (default)
SIGSTOP 19 Pause process
SIGCONT 18 Resume paused process

List all signals

kill -l

Practical examples

# Reload nginx configuration without stopping
sudo kill -HUP $(pgrep nginx | head -1)

# Pause a process
kill -STOP 1234

# Resume a paused process
kill -CONT 1234

# Kill all processes of a user
sudo pkill -u baduser

# Kill all processes matching a pattern
pkill -f "npm start"

Always try SIGTERM first

# Step 1: Ask nicely
kill 1234

# Step 2: Wait a few seconds, check if it's gone
sleep 3 && ps -p 1234

# Step 3: Only if still running, force it
kill -9 1234

Background & Foreground Jobs

Run a command in the background

# Append & to run in background
sleep 100 &
# Output: [1] 5678 (job number and PID)

List background jobs

jobs
# Output: [1]+  Running  sleep 100 &

Manage jobs

# Bring job to foreground
fg %1

# Send running job to background
# 1. Press Ctrl+Z (pause it)
# 2. Then:
bg %1

# Disown a job (won't be killed when terminal closes)
disown %1

nohup — Survive logout

# Run a command that survives logout
nohup ./long-script.sh &

# Output goes to nohup.out by default
# Redirect output
nohup ./script.sh > output.log 2>&1 &

Practical workflow

# 1. Start a long download
wget https://example.com/large-file.iso &

# 2. Check it's running
jobs

# 3. Do other work...

# 4. Bring it back to foreground if needed
fg %1

Measure Execution Time — time

time measures how long a command takes to run.

time ls -la /etc

Output:

real    0m0.012s    ← Wall clock time
user    0m0.004s    ← CPU time in user mode
sys     0m0.008s    ← CPU time in kernel mode

Practical examples

# Time a script
time ./deploy.sh

# Time a build
time npm run build

# Time a database query
time psql -c "SELECT COUNT(*) FROM users;" mydb

# Compare two approaches
time find / -name "*.log" 2>/dev/null
time locate "*.log"

Exercises

Exercise 1: Process inspection

# 1. Show all running processes
ps aux | head -20

# 2. Find the most CPU-intensive processes
ps aux --sort=-%cpu | head -10

# 3. Find the most memory-intensive processes
ps aux --sort=-%mem | head -10

# 4. Show processes as a tree
ps auxf | head -30

# 5. Count total running processes
ps aux | wc -l

Exercise 2: Background jobs

# 1. Start a background process
sleep 60 &

# 2. Check it's running
jobs

# 3. Bring it to foreground
fg %1

# 4. Pause it (Ctrl+Z)
# Press Ctrl+Z

# 5. Resume in background
bg %1

# 6. Kill it
kill %1

Exercise 3: Signals

# 1. Start a process
sleep 300 &
echo "PID: $!"

# 2. Check it's running
ps -p $!

# 3. Send SIGTERM
kill $!

# 4. Verify it's gone
ps -p $! 2>/dev/null || echo "Process terminated"

Key Takeaways

  • ps aux — snapshot of all processes
  • top — live process monitor (basic)
  • htop / btop — interactive process monitor (recommended)
  • kill PID — graceful stop; kill -9 PID — force stop
  • killall name / pkill pattern — kill by name
  • command & — run in background
  • fg / bg — foreground/background control
  • nohup — survive terminal close
  • time command — measure execution time

Next Lesson: Lesson 15: Archives & Compression →