DaleSchool

Process Management

Beginner20min

Learning Objectives

  • Check running processes with ps and top
  • Terminate processes with kill
  • Switch between background and foreground execution
  • Check disk usage with df and du

Working Code

Example 1: Checking running processes

ps

Example output:

  PID TTY           TIME CMD
 1234 ttys001    0:00.05 -zsh
 5678 ttys001    0:00.01 ps

Column meanings:

  • PID: Process ID (unique number for each process)
  • TTY: Terminal connection info
  • TIME: CPU time used
  • CMD: Running command

For more detail:

ps aux

Example output:

USER       PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
dale      1234   0.0  0.1  408952   2056 s001  S   10:00am   0:00.05 -zsh
dale      5678   0.0  0.0  408400    896 s001  R+  10:05am   0:00.01 ps aux

Additional columns:

  • %CPU: CPU usage
  • %MEM: Memory usage
  • STAT: Process state (S=sleeping, R=running, Z=zombie)

Example 2: Finding a specific process

ps aux | grep node

Output:

dale      3456   0.2  1.5 123456  30000 s001  S   09:00am   0:02.10 node server.js

Example 3: Finding which process is using a port

# Find the process using port 3000
lsof -i :3000

Output:

COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
node     3456 dale   23u  IPv6   0x...     0t0  TCP *:3000 (LISTEN)

Check the PID and terminate it:

kill 3456

Try It Yourself

Background Execution

# Append & to run in the background
sleep 30 &

Output:

[1] 7890

[1] is the job number, 7890 is the PID.

# List background jobs
jobs

Output:

[1]  + running    sleep 30
# Bring a background job to the foreground
fg %1

# Pause a foreground job (Ctrl+Z)
# Then resume it in the background
bg %1

Terminating Processes

# Request graceful termination (SIGTERM, default)
kill 7890

# Force immediate termination (SIGKILL, when unresponsive)
kill -9 7890

# Terminate by job number
kill %1

Checking Disk Usage

# Overall disk usage (human-readable)
df -h

Output:

Filesystem     Size   Used  Avail Capacity  Mounted on
/dev/disk1s1  500G   120G   375G    25%    /
/dev/disk1s2  500G   120G   375G    25%    /System/Volumes/Data
# Size of a specific directory
du -sh ~/Documents

Output:

2.1G    /Users/dale/Documents
# Size by subdirectory (top 5)
du -sh ~/Documents/* | sort -rh | head -5

Output:

1.5G    /Users/dale/Documents/Videos
342M    /Users/dale/Documents/Projects
 89M    /Users/dale/Documents/Archive
 12M    /Users/dale/Documents/Docs
4.0K    /Users/dale/Documents/notes.txt

"Why?" — When You Need Process Management

| Scenario | Solution | | --------------------------------------------- | ------------------------------------------------------ | ---------------------------------- | | Dev server unresponsive | ps aux | grep node-> find PID ->kill PID | | Port 3000 already in use | lsof -i :3000 -> find PID -> kill PID | | Need to do other work during a build/download | Run with command & | | Running out of disk space | df -h for overview, du -sh * to find large folders |

kill Signals Reference

| Signal | Number | Meaning | When to Use | | --------- | ------ | --------------------- | ---------------------- | | SIGTERM | 15 | Graceful termination | Default, try first | | SIGKILL | 9 | Forced immediate kill | When unresponsive | | SIGINT | 2 | Interrupt (Ctrl+C) | Foreground termination | | SIGHUP | 1 | Restart | Reload configuration |

Always try kill (SIGTERM) first. Only use kill -9 if the process doesn't respond. SIGKILL gives the process no chance to clean up.

Common Mistakes

Mistake 1: Using kill -9 first

# Bad habit
kill -9 1234   # process can't clean up temp files, sockets, etc.

# Good habit
kill 1234      # try graceful termination first
# if no response, then use -9
kill -9 1234

Mistake 2: Running kill without checking the PID

# Dangerous: might terminate the wrong process
kill 1

# Safe: check first
ps aux | grep node
# ... confirm the PID ...
kill 1234

Mistake 3: Losing background jobs

Closing the terminal also terminates background jobs. To keep a process running after the terminal closes:

# nohup: keep running after terminal closes
nohup python server.py &

# Output is saved to nohup.out
tail -f nohup.out

Deep Dive

Real-time monitoring with top and htop

top shows processes in real time:

top

Controls:

  • q — quit
  • k — kill a process (enter PID)
  • u — filter by user
  • M — sort by memory usage

htop is a more user-friendly alternative:

brew install htop
htop

htop features:

  • Color-coded CPU/memory visualization
  • Mouse-clickable processes
  • Arrow key navigation, F9 to kill
Reading process status codes

The STAT column in ps aux:

| Code | State | | ---- | ---------------------------------------------- | | R | Running | | S | Sleeping (waiting) | | D | Disk wait (I/O wait) | | Z | Zombie (exited but parent hasn't collected it) | | T | Stopped (Ctrl+Z) | | + | Foreground process | | s | Session leader |

Zombie processes (Z) can't be killed directly. Terminate the parent process to clean them up.

  1. Check running bash/zsh processes with ps aux | grep bash.
  2. Start a background job with sleep 30 &, then verify with jobs.
  3. Bring it to the foreground with fg %1, then terminate with Ctrl+C.
  4. Check disk usage with df -h and note how much free space you have.
  5. Check the total size of your home directory with du -sh ~.

Q1. Which kill signal forces immediate process termination?

  • A) kill -1
  • B) kill -9
  • C) kill -15
  • D) kill -2