When a program no longer responds or uses too many resources, you have to
stop it. Linux uses a system of signals to talk to
processes. The kill command sends these signals and, despite its name,
it does not always kill: it asks politely first.
What you will learn
- Stop a process cleanly with
kill - Force a stop with
kill -9 - Understand the signals SIGTERM, SIGKILL and SIGINT
- Target a process by name with
killallandpkill - Get back control of a blocked terminal
Which signals should you know?
A signal is a message sent to a process to ask it to act. Here are the three to know:
| Signal | Number | Effect |
|---|---|---|
| SIGTERM | 15 | Clean stop request (the process can save and close) |
| SIGKILL | 9 | Immediate, forced stop (the process cannot ignore it) |
| SIGINT | 2 | Interruption (same as Ctrl+C) |
How do you stop a process with kill?
kill sends a signal to a process identified by its PID.
-
Find the PID of the process to stop:
Terminal window ps aux | grep program-nameExample bob 1400 2.5 1.0 245680 98200 pts/0 S+ 10:00 0:03 python3 long-script.pyThe PID is
1400. -
Send SIGTERM (clean stop):
Terminal window kill 1400 -
Check that the process is gone:
Terminal window ps -p 1400If only the header line shows, the process has ended. Avoid
ps aux | grep 1400for this check: the line of thegrepitself contains1400and makes you believe the process is still running. -
If the process is still there, force it with SIGKILL:
Terminal window kill -9 1400
kill syntax
kill PID # Sends SIGTERM (signal 15): the defaultkill -15 PID # Same thing: explicit SIGTERMkill -9 PID # Sends SIGKILL: forced stopkill -SIGTERM PID # Same thing with the signal nameHow do you interrupt the current process with Ctrl+C?
When a program runs in the foreground of your terminal:
| Shortcut | Signal sent | Effect |
|---|---|---|
| Ctrl+C | SIGINT (2) | Interruption request |
| Ctrl+Z | SIGTSTP (20) | Suspends the process (does not stop it) |
| Ctrl+\ | SIGQUIT (3) | Stop with a core dump (last resort) |
In most situations, Ctrl+C is enough.
How do you stop processes by name with killall?
killall targets every process carrying a given name:
# Stop every "firefox" processkillall firefox
# Force the stopkillall -9 firefoxHow do you stop processes with a pattern using pkill?
pkill is more flexible, it accepts a partial pattern:
# Stop the processes whose name contains "python"pkill python
# Stop the processes of a given userpkill -u aliceThe difference between killall and pkill:
| Command | Matching |
|---|---|
killall firefox | Exact name: firefox |
pkill fire | Partial pattern: anything containing fire |
How do you get back control of a blocked terminal?
When your terminal seems blocked, try these moves in this order:
-
Ctrl+C, which interrupts the foreground process
-
Ctrl+Z, which suspends the process (then
kill %1to end it) -
If nothing responds, open a second terminal and find the PID:
Terminal window ps aux | grep your-command -
From the second terminal:
Terminal window kill PID -
As a last resort:
Terminal window kill -9 PID
Practical examples
Three situations cover most of what you will do with these commands: a development server left running, several instances of a script, and the final check that a process is really gone.
Stop a development web server
A development server started in the foreground stops with Ctrl+C. But when it runs in the background or in another terminal, you must find its PID before stopping it. The sequence below illustrates the common case: you identify the process by its command line, then send it a SIGTERM for a clean stop.
# Find the PID of the serverps aux | grep "python3 -m http.server"
# Result: PID 2100kill 2100Stop every process of a script
# Several instances of script.sh are runningpkill -f "script.sh"The -f option searches the full command line, not only the
process name.
Check that a process has really stopped
# After killps -p 2100If the process is gone, only the header line shows and the exit code
is 1 on the version of ps tested (check it on your machine with
echo $?, the value can vary with the version). Otherwise, its line appears under the header.
Command summary
This table condenses the whole guide into a cheat sheet. Keep in mind the
progression it implies: you go from the gentlest (SIGTERM, Ctrl+C) to the most
brutal (SIGKILL), and from the most precise (a PID) to the broadest (killall,
pkill). When in doubt, always start with the top line.
| Command | Action |
|---|---|
kill PID | Sends SIGTERM to the process (clean stop) |
kill -9 PID | Sends SIGKILL (forced stop) |
killall name | Stops every process named exactly name |
pkill pattern | Stops the processes matching the pattern |
pkill -f "command" | Searches the full command line |
Ctrl+C | Interrupts the foreground process |
Ctrl+Z | Suspends the foreground process |
Troubleshooting
When a kill has no effect, the cause is almost always one of the five
lines below. The useful reflex is to read the reason before forcing:
a process that ignores SIGTERM is dealt with by SIGKILL, but a process in state
D (waiting on disk or network) answers no signal, and insisting is
useless until the blocking access is resolved.
| Symptom | Likely cause | Fix |
|---|---|---|
kill PID does nothing | The process ignores SIGTERM | Wait a few seconds, then kill -9 PID |
kill: (PID) - Operation not permitted | The process belongs to another user | Use sudo kill PID |
killall does not find the process | The name does not match exactly | Use pkill with a partial pattern |
| A zombie process persists | The parent has not collected the exit code | Kill the parent process or wait |
kill -9 does not work | Process in state D (kernel I/O wait), very rare | Solve the I/O problem (disk, network) or reboot |
Key points
- SIGTERM (
kill PID) asks for a clean stop: always try it first. - SIGKILL (
kill -9 PID) forces the stop: a last resort. - Ctrl+C sends SIGINT to the foreground process.
killalltargets an exact name,pkilla partial pattern.- Always check with
ps -pthat the process has really gone.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Install and maintain a Linux system
A new full quiz with random questions
Work again only on the questions you missed