How to kill a process by its pid on Linux?
Categories:
Mastering Process Termination: How to Kill a Process by PID on Linux

Learn the essential Linux commands to identify and terminate processes using their Process ID (PID), ensuring system stability and resource management.
Managing processes is a fundamental skill for any Linux user or administrator. Whether a program is unresponsive, consuming excessive resources, or simply needs to be stopped, knowing how to terminate it gracefully (or forcefully) is crucial. This article will guide you through the various methods of killing a process by its Process ID (PID) on Linux, explaining the different signals and best practices.
Understanding Processes and PIDs
Every running program or command on a Linux system is considered a process. Each process is assigned a unique numerical identifier called a Process ID (PID). This PID is essential for interacting with and managing individual processes. When you want to stop a specific application, you'll typically need its PID first.
Finding a Process's PID
Before you can kill a process, you need to identify its PID. There are several common commands to achieve this, depending on what information you have about the process.
ps aux | grep <process_name>
Using ps
and grep
to find a process by name.
The ps aux
command lists all running processes. Piping its output to grep
allows you to filter for a specific process name. Look for the second column in the output, which represents the PID.
pgrep <process_name>
Using pgrep
for a simpler PID lookup.
The pgrep
command is a more direct way to find PIDs based on a process name or other attributes. It returns only the PIDs, making it convenient for scripting.
The kill
Command and Signals
The primary command for terminating processes is kill
. However, kill
doesn't just 'kill' in the literal sense; it sends a signal to a process. Different signals instruct the process to behave in different ways. The most common signals used for termination are:
- SIGTERM (15): The default and most graceful way to terminate a process. It requests the process to shut down, allowing it to clean up resources and save data before exiting.
- SIGKILL (9): The most forceful way to terminate a process. It immediately stops the process without giving it a chance to clean up. Use this as a last resort for unresponsive processes.
- SIGHUP (1): Often used to tell a process to re-read its configuration files without restarting. While not a termination signal, it's useful for managing long-running services.
flowchart TD A[Start Process] --> B{Process Running} B --> C{Send SIGTERM (15)} C --> D{"Process handles signal?"} D -->|Yes| E[Graceful Shutdown] D -->|No| F{Process still running?} F -->|Yes| G{Send SIGKILL (9)} G --> H[Immediate Termination] E --> I[Process Exited] H --> I
Process termination flow using SIGTERM and SIGKILL.
Terminating Processes by PID
Once you have the PID, you can use the kill
command to send signals.
kill <PID>
# This sends SIGTERM (signal 15) by default
kill -15 <PID>
# Explicitly sends SIGTERM
kill -SIGTERM <PID>
# Another way to explicitly send SIGTERM
Gracefully terminating a process using kill
with SIGTERM.
For unresponsive processes that don't respond to SIGTERM, you'll need to use SIGKILL.
kill -9 <PID>
# Forcefully terminates the process using SIGKILL
kill -SIGKILL <PID>
# Another way to forcefully terminate
Forcefully terminating a process using kill
with SIGKILL.
kill -9
(SIGKILL) should be a last resort. It does not allow the process to clean up, which can lead to data corruption or orphaned files. Always try kill <PID>
(SIGTERM) first.Other Useful Commands for Process Termination
Beyond kill
, there are other commands that simplify process termination, especially when dealing with processes by name.
killall <process_name>
# Sends SIGTERM to all processes with the specified name
killall -9 <process_name>
# Forcefully kills all processes with the specified name
Using killall
to terminate processes by name.
The killall
command is useful when you want to terminate all instances of a particular program without having to find each individual PID. Be cautious when using killall
, especially with -9
, as it can affect multiple processes.
pkill <process_name>
# Sends SIGTERM to processes matching the name
pkill -9 -f <pattern>
# Forcefully kills processes matching a pattern in the full command line
Using pkill
for flexible process termination.
pkill
is similar to pgrep
but directly sends a signal. It supports more advanced pattern matching, including matching against the full command line (-f
option), which can be very powerful.
grep
or pkill
with process names, be specific to avoid accidentally killing critical system processes. For example, grep firefox
is safer than grep fire
.Practical Steps for Terminating a Stuck Process
Here's a common workflow for dealing with an unresponsive application:
1. Identify the Process
Open a terminal and use ps aux | grep <app_name>
or pgrep <app_name>
to find the PID of the unresponsive application. Make sure to note down the correct PID.
2. Attempt Graceful Termination
Try to terminate the process gracefully first by running kill <PID>
. Wait a few seconds to see if the application closes.
3. Verify Termination
Check if the process is still running using ps aux | grep <app_name>
or pgrep <app_name>
. If no output is returned, the process has been terminated.
4. Forceful Termination (If Necessary)
If the process is still running after attempting graceful termination, use kill -9 <PID>
to forcefully stop it. Again, verify its termination.