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:

Bash

Example output:

text

2. Basic kill (polite way – default signal = SIGTERM = -15)

Bash

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:

Bash

You get a list like:

text

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)

Bash

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:

Bash

Now play:

Bash

Real dangerous but common:

Bash

Kill by name (easier sometimes):

Bash

6. Kill process groups (advanced but useful)

If a process spawns children (e.g. script starts many tasks):

Bash

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:

Bash

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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *