Chapter 32: Bash Terminate kill
What does kill actually do? (super simple first)
kill sends a signal (a special message) to one or more running processes (programs/tasks) by their PID (Process ID).
The default signal is SIGTERM (terminate nicely) → most programs listen to it, clean up (save files, close connections), and exit politely.
But you can send other signals like:
- Ask nicely → “please stop” (SIGTERM)
- Force stop immediately → “die now, no cleanup” (SIGKILL)
- Reload config → “re-read your settings” (SIGHUP)
- Interrupt like Ctrl+C → “stop what you’re doing” (SIGINT)
kill is not always violent – it’s a polite way to communicate with processes.
Why do we need kill?
- A program hangs → won’t close normally
- High CPU/RAM eater → stop it fast
- Background script stuck → terminate it
- Service needs restart → send reload signal
- Clean shutdown of servers/apps
1. First – find the PID (you need this!)
Use any of these:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
ps aux | grep firefox # find PID of firefox # or better (avoid grep self) ps aux | grep [f]irefox # or pgrep firefox # just shows PIDs # or in htop / top → look at PID column |
Example output:
|
0 1 2 3 4 5 6 7 |
arman 5678 4.2 3.1 1890123 345678 ? Sl 15:10 1:23 /usr/bin/firefox # PID = 5678 |
2. Basic kill (polite way – default signal = SIGTERM = -15)
|
0 1 2 3 4 5 6 |
kill 5678 # ask firefox to close nicely |
Most GUI apps (firefox, code, spotify) will listen → save tabs, close windows, exit.
If it ignores → use stronger signals.
3. Most common signals (learn these 4 first!)
Run this to see all signals:
|
0 1 2 3 4 5 6 |
kill -l |
You get a list like:
|
0 1 2 3 4 5 6 7 8 9 |
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 ... 15) SIGTERM ... |
Top 5 signals you will use 99% of time:
| Number | Name | Short name | What it does | When to use | Can process ignore? |
|---|---|---|---|---|---|
| 15 | SIGTERM | TERM | “Terminate” – polite, ask to clean up & exit | Normal stop, graceful shutdown | Yes |
| 9 | SIGKILL | KILL | “Kill immediately” – no cleanup, forced death | Hung process, won’t die with TERM | No (cannot ignore) |
| 2 | SIGINT | INT | “Interrupt” – same as Ctrl+C in terminal | Like pressing Ctrl+C | Yes |
| 1 | SIGHUP | HUP | “Hangup” – reload config or re-read files | Restart services without full stop (nginx, sshd) | Yes |
| 19/18/23 | SIGSTOP | STOP | “Stop/Pause” – freeze process (like Ctrl+Z) | Temporarily pause (use fg to resume) | No |
4. How to send signals (3 ways – all same effect)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Way 1: Number (most common & fast) kill -9 5678 # force kill firefox kill -15 5678 # polite terminate (same as plain kill) # Way 2: Name without SIG kill -KILL 5678 kill -TERM 5678 # Way 3: Full SIG name kill -SIGKILL 5678 kill -SIGTERM 5678 |
Pro tip: Most people use numbers → -9 for force, -15 or nothing for polite.
5. Real examples you should try (safe ones!)
First – start a harmless background sleep:
|
0 1 2 3 4 5 6 |
sleep 1000 & # runs in background, PID shown e.g. [1] 12345 |
Now play:
|
0 1 2 3 4 5 6 7 8 9 10 |
ps | grep sleep # see PID e.g. 12345 kill 12345 # polite – it will exit # or kill -9 12345 # force – instant death |
Real dangerous but common:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
# Hung python script using 100% CPU? ps aux --sort=-%cpu | head -5 # see PID e.g. 7890 kill -15 7890 # give chance to clean # wait 5 sec, if still running: kill -9 7890 # no mercy |
Kill by name (easier sometimes):
|
0 1 2 3 4 5 6 7 8 |
pkill firefox # sends SIGTERM to all firefox processes pkill -9 python3 # force kill all python3 killall -9 node # same as pkill, by exact name |
6. Kill process groups (advanced but useful)
If a process spawns children (e.g. script starts many tasks):
|
0 1 2 3 4 5 6 7 |
# Negative PID = kill process group kill -9 -1234 # kill group with leader PID 1234 |
7. Quick cheat-sheet table
| Goal | Command example | Notes |
|---|---|---|
| Polite stop | kill PID or kill -15 PID | Default – graceful |
| Force kill (hung process) | kill -9 PID or kill -KILL PID | No cleanup – use last |
| Like Ctrl+C | kill -2 PID or kill -INT PID | Interrupt |
| Reload config (nginx, etc.) | kill -1 PID or kill -HUP PID | No restart needed |
| Pause / freeze | kill -STOP PID | Resume with kill -CONT PID |
| List all signals | kill -l | See names & numbers |
| Kill by name (polite) | pkill firefox or killall firefox | Easier than PID |
| Force kill by name | pkill -9 python | All matching processes |
8. Safety rules (very important!)
- Always try -15 (or no number) first → gives program chance to save data
- Use -9 only when -15 fails → it can cause data loss/corruption
- Never kill -9 system processes (PID < 1000) or init/systemd (PID 1)
- Check PID twice – wrong PID = you kill wrong program!
- For services → better use systemctl stop nginx instead of kill
Now open your terminal and try safely:
|
0 1 2 3 4 5 6 7 8 |
sleep 500 & # start dummy process ps | grep sleep # note PID kill <that_PID> # polite stop |
Tell me: Did it exit nicely? Or ask:
- “How to kill all chrome tabs except one?”
- “Difference between kill -9 and pkill -9?”
- “What happens if I kill -9 a database server?”
We’ll practice together! 😄
