Killing Processes

Sometimes processes misbehave. They freeze, eat CPU, or just won't quit. Time to learn process termination.

kill - Send Signals to Processes

Despite its name, kill actually sends signals. Termination is just one option.

Terminal
$kill 1234
(sends SIGTERM to PID 1234)

Default signal is SIGTERM (15) - a polite "please stop."

Finding the PID

Terminal
$ps aux | grep firefox
user 1234 5.0 8.2 123456 98765 ? Sl 10:00 2:30 firefox
$kill 1234

Or use pgrep:

Terminal
$pgrep firefox
1234
$kill $(pgrep firefox)

SIGTERM vs SIGKILL

Terminal
$kill 1234
(SIGTERM - polite request)
$kill -9 1234
(SIGKILL - forced termination)
SignalNumberEffect
SIGTERM15Polite request to stop
SIGKILL9Forced termination

SIGKILL is Dangerous

SIGKILL (kill -9) doesn't let the process clean up. Use it only if SIGTERM doesn't work. Try SIGTERM first, wait, then SIGKILL.

killall - Kill by Name

Terminal
$killall firefox
(kills all processes named firefox)

Easier than finding PIDs. But be careful - it kills ALL matching processes.

pkill - Pattern-based Kill

Terminal
$pkill fire
(kills anything matching 'fire')
$pkill -u john
(kills all processes owned by john)

More flexible than killall.

The Kill Workflow

  1. Try SIGTERM first (default)

    hljs bash
    kill PID
    
  2. Wait a few seconds - check if process stopped

  3. If still running, SIGKILL

    hljs bash
    kill -9 PID
    

Why This Order?

SIGTERM lets the process:

  • Save data
  • Clean up temp files
  • Close network connections
  • Release resources properly

SIGKILL just yanks the plug.

Common Kill Scenarios

Frozen Application

Terminal
$pgrep -f 'stuck_app'
1234
$kill 1234
$# Wait...
$pgrep -f 'stuck_app'
1234
$# Still there? Force it:
$kill -9 1234

Multiple Instances

Terminal
$killall node
(kills all node processes)

All User's Processes

Terminal
$pkill -u username
(logs out user completely)

Keyboard Interrupts

In the terminal, these also send signals:

ShortcutSignalEffect
Ctrl+CSIGINTInterrupt (usually stops)
Ctrl+ZSIGTSTPSuspend (stop but don't kill)
Ctrl+\SIGQUITQuit with core dump
Terminal
$sleep 100
$# Press Ctrl+C
^C
Knowledge Check

A process isn't responding to `kill PID`. What should you try next?

Quick Reference

CommandEffect
kill PIDSend SIGTERM
kill -9 PIDSend SIGKILL (force)
killall nameKill all by name
pkill patternKill by pattern
pkill -9 patternForce kill by pattern
Ctrl+CInterrupt foreground

Key Takeaways

  • kill sends signals (SIGTERM by default)
  • Always try SIGTERM before SIGKILL
  • SIGKILL (-9) is forceful - use as last resort
  • killall and pkill kill by name/pattern
  • Ctrl+C sends SIGINT to foreground process

Next: understanding more signals.