System Administration · #13 of 19

Process Management

ps, top, kill

What is a Process?

A process is a running instance of a program:


Viewing Processes

ps — Process Snapshot

ps                          # Your current terminal's processes
ps aux                      # All processes, detailed
ps -ef                      # All processes, different format
ps aux | grep firefox       # Find specific process

Understanding ps aux Output

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 169584 11148 ?        Ss   10:00   0:01 /sbin/init
alice     1234  2.0  3.5 3458920 286844 ?      Sl   10:05   1:20 /usr/bin/firefox

Process States


top — Live Process Monitor

top                         # Interactive process viewer

top Keyboard Controls

| Key | Action | |-----|--------| | q | Quit | | k | Kill a process (enter PID) | | M | Sort by memory | | P | Sort by CPU | | u | Filter by user | | h | Help |

htop — Better Alternative

sudo apt install htop
htop

More colorful and user-friendly!


Killing Processes

kill — Send Signal to Process

kill 1234                   # Send SIGTERM (gentle stop)
kill -9 1234                # Send SIGKILL (force kill)
kill -STOP 1234             # Pause process
kill -CONT 1234             # Resume process

Common Signals

| Signal | Number | Effect | |--------|--------|--------| | SIGTERM | 15 | Ask nicely to terminate | | SIGKILL | 9 | Force kill (cannot be caught) | | SIGSTOP | 19 | Pause | | SIGCONT | 18 | Resume | | SIGHUP | 1 | Hangup (often: reload config) |

killall — Kill by Name

killall firefox             # Kill all Firefox processes
killall -9 firefox          # Force kill all Firefox

pkill — Kill by Pattern

pkill -f "python script.py" # Kill process matching pattern

Background & Foreground

Running in Background

command &                   # Start in background
./long_script.sh &          # Run script in background

Job Control

jobs                        # List background jobs
fg                          # Bring last job to foreground
fg %1                       # Bring job #1 to foreground
bg                          # Resume stopped job in background

Ctrl Shortcuts


pgrep — Find Process IDs

pgrep firefox               # PIDs of Firefox processes
pgrep -u alice              # All processes by alice
pgrep -l ssh                # PIDs and names

nice & renice — Priority

Lower nice = higher priority (range: -20 to 19)

nice -n 10 ./cpu_heavy.sh   # Start with lower priority
sudo renice -n -5 1234      # Increase priority of running process

Try It!

Practice process management in the terminal:

Exercises:

  1. View all processes: ps aux
  2. Find a specific process: ps aux | grep bash
  3. See your PID: echo $$
  4. Check process tree: pstree

Try it in the terminal

A sandboxed shell with this lesson’s commands. Type help to see what’s available.

bash — try it