Stopping a process in Linux begins with using the kill command followed by its PID. Understanding how to kill process Linux is a fundamental skill for anyone managing a Linux system, whether you’re a developer, sysadmin, or enthusiast. This guide walks you through every method, from basic commands to advanced signals, ensuring you can handle any stuck or misbehaving process with confidence.
You might have a program that freezes, a service that consumes too much memory, or a background task you need to stop. Whatever the reason, Linux offers multiple ways to terminate processes. Let’s start with the simplest approach and build up to more powerful techniques.
What Is A Process In Linux
A process is simply a running instance of a program. Every program you launch—like a web browser, text editor, or server—creates one or more processes. Each process has a unique numeric identifier called a PID (Process ID). You can think of the PID as a name tag for that running program.
Processes can be in different states: running, sleeping, stopped, or zombie. When something goes wrong, you often need to intervene. Knowing how to kill process Linux means you can regain control of your system quickly.
Finding The Process ID (PID)
Before you can kill a process, you need to know its PID. Linux provides several commands to find this information. The most common ones are ps, pgrep, and top.
Using The Ps Command
The ps command lists running processes. For a full list, type:
ps aux
This shows every process with its user, PID, CPU usage, memory usage, and command name. You can also search for a specific process:
ps aux | grep firefox
This filters the output to show only lines containing “firefox”. The PID is in the second column.
Using Pgrep For Quick Searches
The pgrep command is faster for finding PIDs by name:
pgrep firefox
It returns only the numeric PIDs. If multiple instances run, you’ll see multiple numbers.
Using Top Or Htop
The top command gives a real-time view of processes. Press q to exit. htop is a more user-friendly version with color and mouse support. Install it with your package manager if needed.
How To Kill Process Linux Using The Kill Command
The kill command sends a signal to a process. By default, it sends SIGTERM (signal 15), which asks the process to terminate gracefully. If the process doesn’t respond, you can use stronger signals.
Basic Kill Syntax
kill [signal] PID
For example, to send SIGTERM to process with PID 1234:
kill 1234
If you don’t specify a signal, SIGTERM is used. This is the polite way to stop a process.
Using Kill With Different Signals
Sometimes a process ignores SIGTERM. You then escalate to SIGKILL (signal 9), which forcefully terminates the process:
kill -9 1234
Other useful signals include:
- SIGHUP (1): Hang up, often used to reload configuration
- SIGINT (2): Interrupt, like pressing Ctrl+C
- SIGQUIT (3): Quit with core dump
- SIGSTOP (19): Pause a process
- SIGCONT (18): Resume a paused process
How To Kill Process Linux By Name Using Pkill
The pkill command lets you kill processes by name instead of PID. This is extremely handy when you don’t want to look up the number.
pkill firefox
This sends SIGTERM to all processes named “firefox”. To use a different signal:
pkill -9 firefox
Be careful—pkill matches the process name exactly. If you have multiple similar names, use pgrep -l first to see what will be affected.
How To Kill Process Linux With Killall
The killall command is similar to pkill but uses the exact process name. It kills all instances of that name.
killall firefox
You can also specify a signal:
killall -9 firefox
One difference: killall may not be installed by default on some minimal Linux systems. Install it with sudo apt install psmisc on Debian-based distros.
Using Xkill For Graphical Applications
If you’re using a desktop environment, xkill is the easiest way to kill a misbehaving window. Just run:
xkill
Your cursor changes to a skull and crossbones. Click on the window you want to close, and it dies instantly. This sends SIGKILL to the process behind that window.
How To Kill Process Linux With Signals Explained
Signals are how the kernel communicates with processes. Each signal has a number and a name. Here are the most important ones for killing processes:
| Signal | Number | Description |
|---|---|---|
| SIGHUP | 1 | Hang up, often reloads config |
| SIGINT | 2 | Interrupt from keyboard (Ctrl+C) |
| SIGQUIT | 3 | Quit with core dump |
| SIGTERM | 15 | Terminate gracefully (default) |
| SIGKILL | 9 | Force kill, cannot be ignored |
| SIGSTOP | 19 | Pause the process |
| SIGCONT | 18 | Resume a paused process |
Always try SIGTERM first. It gives the process a chance to clean up files, close connections, and save state. SIGKILL is the nuclear option—use it only when nothing else works.
How To Kill Process Linux Using The Top Command
You can also kill processes directly from within top or htop. This is useful when you’re monitoring system resources.
Killing From Top
Run top, then press k. Top asks for the PID. Type it and press Enter. Then it asks for the signal number. Press Enter for SIGTERM (15) or type 9 for SIGKILL.
Killing From Htop
In htop, navigate to the process using arrow keys. Press F9 to open the kill menu. Choose the signal and press Enter. Htop is more intuitive for beginners.
How To Kill Process Linux That Won’t Die
Sometimes a process refuses to die even after SIGKILL. This usually means it’s stuck in an uninterruptible sleep state (D state), often due to a kernel issue or broken hardware. In this case, a reboot may be the only solution.
Before rebooting, try these steps:
- Send SIGTERM first:
kill 1234 - Wait a few seconds
- Send SIGKILL:
kill -9 1234 - Check if it’s still running:
ps aux | grep 1234 - If it’s a zombie (Z state), the parent process must reap it. You may need to kill the parent or restart the service.
Zombie processes are already dead but remain in the process table because the parent hasn’t read their exit status. Killing the parent usually cleans them up.
How To Kill Process Linux As Root
Some processes belong to other users or system services. To kill them, you need root privileges. Use sudo:
sudo kill 1234
Or for pkill:
sudo pkill -9 apache2
Be extremely careful when killing system processes. Accidentally killing init or systemd can crash your system.
How To Kill Process Linux By Port Number
Sometimes you know a process is using a specific port, but you don’t know its name or PID. Use fuser or lsof to find and kill it.
Using Fuser
fuser -k 8080/tcp
This kills all processes using TCP port 8080. Add -9 for SIGKILL:
fuser -k -9 8080/tcp
Using Lsof
First find the PID:
lsof -i :8080
Then kill it with the PID you see.
How To Kill Process Linux In A Script
You can automate killing processes in shell scripts. Here’s a simple example:
#!/bin/bash
PID=$(pgrep -f "myapp")
if [ -n "$PID" ]; then
kill $PID
echo "Killed process $PID"
else
echo "Process not found"
fi
This finds the PID of a process matching “myapp” and kills it. The -f flag in pgrep matches the full command line, not just the process name.
How To Kill Process Linux With Graceful Shutdown
For services like web servers or databases, always attempt a graceful shutdown first. Many services have their own control scripts:
sudo systemctl stop nginx
Or use the service command:
sudo service mysql stop
These scripts send the appropriate signals and wait for the service to finish its work. Only resort to kill -9 if the service is completely hung.
How To Kill Process Linux Using Systemd
If you’re on a modern Linux distribution with systemd, you can manage services with systemctl. To stop a service:
sudo systemctl stop apache2
To kill a specific unit’s processes forcefully:
sudo systemctl kill apache2
You can also send specific signals:
sudo systemctl kill -s SIGKILL apache2
Common Mistakes When Killing Processes
Even experienced users make errors. Here are pitfalls to avoid:
- Killing the wrong PID—always double-check with
psorpgrep - Using SIGKILL too early—always try SIGTERM first
- Killing system processes—this can crash your system
- Forgetting sudo—you may not have permission to kill another user’s process
- Typing the signal number wrong—
kill -9is correct, notkill -09
How To Kill Process Linux On Remote Servers
When managing remote servers via SSH, the same commands work. Just SSH into the server first:
ssh user@server
ps aux | grep badprocess
kill -9 1234
You can also combine commands in one line:
ssh user@server 'kill -9 $(pgrep badprocess)'
Be cautious—a typo could kill the wrong process on a production server.
How To Kill Process Linux With Python Or Other Languages
You can also kill processes programmatically. In Python, use the os module:
import os
import signal
os.kill(1234, signal.SIGTERM)
Or use subprocess to run shell commands:
import subprocess
subprocess.run(['kill', '1234'])
This is useful for automation scripts that monitor and manage processes.
How To Kill Process Linux: A Quick Reference
Here’s a cheat sheet for common scenarios:
- Kill by PID:
kill 1234 - Force kill by PID:
kill -9 1234 - Kill by name:
pkill firefox - Force kill by name:
pkill -9 firefox - Kill all instances:
killall firefox - Kill by port:
fuser -k 8080/tcp - Kill graphical window:
xkill - Kill from top: press
k - Kill from htop: press
F9 - Kill service:
sudo systemctl stop service
How To Kill Process Linux Safely
Safety should always come first. Follow these guidelines:
- Identify the exact process you want to kill
- Try graceful termination first (SIGTERM)
- Wait a few seconds for the process to respond
- If it doesn’t stop, escalate to SIGKILL
- Never kill processes you don’t understand
- On production systems, notify your team before killing critical services
How To Kill Process Linux: Troubleshooting
If a process won’t die, check these things:
- Is it a zombie? Check with
ps aux | grep Z - Is it in uninterruptible sleep? Check the STAT column in
ps - Do you have permission? Use
sudo - Is the PID correct? Double-check with
pgrep - Is the process a kernel thread? These cannot be killed
Kernel threads appear with brackets in ps output, like [kworker/0:0]. They are managed by the kernel and cannot be killed by users.
How To Kill Process Linux With Signals In Practice
Let’s walk through a real example. Suppose Firefox freezes. Here’s what you do:
- Open a terminal
- Run
pgrep firefoxto get the PID (say 4567) - Send SIGTERM:
kill 4567 - Wait 5 seconds
- Check if it’s still running:
ps -p 4567 - If it’s still there, send SIGKILL:
kill -9 4567
Alternatively, use one command: pkill -9 firefox
How To Kill Process Linux: Advanced Techniques
For power users, here are some advanced methods:
Killing Process Trees
Sometimes you need to kill a process and all its children. Use kill with negative PID:
kill -TERM -1234
This sends the signal to the process group. Be careful—it kills all processes in that group.
Using Nice And Renice
Before killing a process that’s using too much CPU, you can try lowering its priority with renice:
renice +10