The kill Command — Terminating Processes in Linux¶
The
killcommand is used to send signals to running processes. Although its name suggests that it always terminates processes,killcan send many different signals, allowing you to gracefully stop, pause, resume, or forcefully terminate applications. It is one of the most important commands for Linux administrators, DevOps engineers, Cloud Architects, and Site Reliability Engineers (SREs).
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
killcommand - Find Process IDs (PIDs)
- Send signals to processes
- Gracefully terminate applications
- Forcefully terminate processes
- Kill multiple processes
- Troubleshoot stuck applications
- Apply process termination safely in production
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management and Permissions
- Module 5 – Users and Groups
- Module 6 Lessons 1–6
Why Learn kill?¶
Imagine:
- A Java application stops responding.
- NGINX hangs.
- A backup process never finishes.
- A Python script enters an infinite loop.
How do you stop the process?
Linux provides the:
command.
What is kill?¶
Despite its name,
kill does not always kill a process.
Instead,
it sends a signal to a process.
The process decides how to respond depending on the signal.
Basic Syntax¶
Example:
By default,
this sends:
Finding the PID¶
Before terminating a process,
find its PID.
Example:
or
Example:
Terminate:
Graceful Termination¶
Default signal:
Example:
The application has an opportunity to:
- Save data
- Close files
- Release resources
- Shut down cleanly
This is the preferred method.
Forceful Termination¶
Some processes ignore SIGTERM.
Use:
Example:
This sends:
The process is terminated immediately by the kernel and cannot clean up resources.
Use only as a last resort.
Kill Multiple Processes¶
Example:
Kill by Signal Name¶
Instead of numbers,
use names.
Example:
or
Display Available Signals¶
Example:
Verify the Process¶
Before:
Terminate:
Verify again:
If no output is displayed,
the process has exited.
Common Signals¶
| Signal | Number | Purpose |
|---|---|---|
| SIGTERM | 15 | Graceful termination |
| SIGKILL | 9 | Immediate termination |
| SIGINT | 2 | Interrupt (Ctrl + C) |
| SIGSTOP | 19 | Pause a process |
| SIGCONT | 18 | Resume a paused process |
| SIGHUP | 1 | Reload or restart configuration (application-dependent) |
We'll learn signals in detail in the next lesson.
Common Commands¶
Terminate process.
Force termination.
List signals.
Terminate multiple processes.
Terminate gracefully.
Real Production Examples¶
Stop NGINX worker.
Terminate a stuck Python script.
Stop a backup job.
Terminate a runaway Java process.
Production Perspective¶
The kill command is used for:
- Hung applications
- High CPU processes
- Memory leaks
- Infinite loops
- Failed deployments
- Troubleshooting
- Maintenance
- Incident response
Always attempt graceful termination before using SIGKILL.
Hands-on Lab¶
Task 1¶
Start a process.
Task 2¶
Find its PID.
Task 3¶
Terminate gracefully.
Task 4¶
Verify termination.
Task 5¶
Start another process.
Task 6¶
Force termination.
Task 7¶
List available signals.
Task 8¶
Terminate multiple test processes.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
kill PID | Gracefully terminate | Application shutdown |
kill -9 PID | Force termination | Hung processes |
kill -15 PID | Send SIGTERM | Controlled shutdown |
kill -l | List signals | Signal reference |
ps -p PID | Verify process | Troubleshooting |
Production Troubleshooting Scenario¶
Scenario
A Java application begins consuming 100% CPU.
Investigation:
The application stops responding.
First attempt:
The application does not exit.
Second attempt:
The process terminates immediately.
The administrator then investigates the application logs to determine the root cause rather than relying on forceful termination as a permanent solution.
Best Practices¶
- Always verify the PID before terminating a process.
- Use
SIGTERM(kill) beforeSIGKILL(kill -9). - Confirm the process owner and purpose before terminating it.
- Investigate why a process became unresponsive.
- Avoid using
kill -9unless graceful termination fails.
Common Mistakes¶
❌ Using kill -9 as the first option.
✅ Always try graceful termination first.
❌ Killing the wrong process due to an incorrect PID.
✅ Double-check the PID before sending signals.
❌ Terminating critical system processes.
✅ This can destabilize the operating system or interrupt running services.
Interview Questions¶
Beginner¶
- What does the
killcommand do? - Which signal is sent by default?
- What is the difference between
killandkill -9? - How do you list available signals?
Intermediate¶
- Why is
SIGTERMpreferred overSIGKILL? - How do you terminate multiple processes?
- How do you verify that a process has terminated?
- Why might a process ignore
SIGTERM?
Architect Level¶
- How would you safely terminate a production application?
- Why should
SIGKILLbe used only as a last resort? - How would you investigate an application that repeatedly requires forceful termination?
Summary¶
In this lesson, you learned:
- The
killcommand - Process termination
- Graceful shutdown
- Forceful termination
- Process signals
- Production troubleshooting
- Best practices
The kill command is a fundamental Linux administration tool. It provides controlled communication with running processes through signals, allowing administrators to terminate or manage applications safely. Understanding when to use graceful versus forceful termination is essential for maintaining stable production systems.
Key Takeaways¶
killsends signals to processes.- The default signal is SIGTERM (15).
- Use SIGTERM for graceful shutdowns.
- Use SIGKILL (9) only when a process cannot terminate normally.
- Verify the PID before sending signals.
- Always investigate the root cause of hung or unresponsive processes.
What's Next?¶
Linux Signals — Process Communication and Control
You'll explore:
- What signals are
- Common Linux signals
- Signal numbers and names
- Signal handling
- Sending signals with
kill - Process communication
- Production use cases
Understanding signals will give you deeper insight into how Linux processes communicate and respond to system events.