Killing multiple processes in Linux can be done efficiently with the pkill command and a pattern. If you’ve ever needed to stop several running programs at once, you know that clicking through a task manager or typing kill commands one by one is a waste of time. This guide covers exactly how to kill multiple processes in Linux using simple, powerful commands that save you effort and keep your system running smoothly.
Whether you’re managing a server or your personal desktop, killing processes in bulk is a common task. Let’s jump right into the most effective methods, from the basic pkill to advanced scripting techniques.
How To Kill Multiple Processes In Linux
Before we get into the details, understand that Linux gives you several tools for this job. The most popular ones are pkill, killall, and xargs combined with ps or pgrep. Each has its strengths, and you’ll learn when to use each one.
Using The Pkill Command
The pkill command is your best friend for killing processes by name. It matches process names against a pattern and sends a signal to all matching processes.
- Open a terminal.
- Type
pkill firefoxto kill all Firefox processes. - Use
pkill -9 firefoxto force kill them immediately.
This command is case-sensitive by default. If you’re not sure about the exact case, add the -i flag to ignore case: pkill -i firefox.
One common mistake is that pkill matches the process name exactly or partially. For example, pkill ssh might kill both ssh and sshd processes. To avoid this, use the -x flag to match only exact names: pkill -x ssh.
Using The Killall Command
killall is similar to pkill but requires an exact process name. It’s more precise and less likely to accidentally kill unrelated processes.
killall firefoxkills all processes named exactly “firefox”.killall -9 firefoxsends SIGKILL to force termination.killall -I firefoxignores case (uppercase -I).
Note that killall on some systems (like BSD/macOS) behaves differently, but on Linux it works as described here.
Killing Processes By User
Sometimes you need to kill all processes owned by a specific user. This is useful when a user logs out but leaves processes running.
Use pkill -u username to kill all processes for that user. For example:
pkill -u john kills every process owned by the user “john”. Be careful with this command—it can log out the user and cause data loss.
To see what would be killed before acting, use pgrep -u john to list the process IDs.
Killing Processes By Terminal
If you want to kill all processes running in a specific terminal session, use the tty command to find your terminal and then pkill -t pts/0 (replace with your terminal).
This is handy when a terminal is stuck and you can’t close it normally.
Using Xargs With Ps Or Pgrep
For more complex filtering, combine ps or pgrep with xargs. This gives you full control over which processes to kill.
- List processes with
ps aux | grep pattern. - Pipe the output to
awkto extract process IDs. - Use
xargs killto kill them.
Example: Kill all processes matching “myapp”:
ps aux | grep myapp | awk '{print $2}' | xargs kill
This is a three-step pipeline that finds, filters, and kills. Always test with echo first:
ps aux | grep myapp | awk '{print $2}' | xargs echo
This shows you the PIDs without killing them.
Using Pgrep And Pkill Together
pgrep lists process IDs matching a pattern, while pkill kills them. You can use them together for safety:
pgrep -f "python script.py" shows the PIDs.
pkill -f "python script.py" kills them.
The -f flag matches the full command line, not just the process name. This is useful for killing specific scripts or commands.
Killing Processes By Age Or Resource Usage
Advanced users can kill processes based on how long they’ve been running or how much CPU/memory they use. This requires scripting with ps and awk.
Example: Kill processes older than 1 hour:
ps -eo pid,etime,comm | awk '{if($2 ~ /:/ && $2 > 60) print $1}' | xargs kill
This parses the elapsed time and kills processes running for more than 60 minutes. Be careful—this can kill important system processes if misused.
Using The Kill Command With A List
If you have a list of process IDs in a file, you can kill them all with:
kill -9 $(cat pids.txt)
Or using xargs:
cat pids.txt | xargs kill -9
This is useful when you generate a list from a script or log file.
Killing Processes With Fuser
fuser identifies processes using a file or socket. To kill all processes accessing a specific file:
fuser -k /path/to/file
This is great for unmounting filesystems or freeing locked files.
Safety Tips And Best Practices
Killing processes can cause data loss or system instability. Always follow these guidelines:
- Use
SIGTERM(signal 15) first, which allows processes to clean up. - Use
SIGKILL(signal 9) only as a last resort. - Test with
pgreporechobefore killing. - Avoid killing system processes unless you know what you’re doing.
- Use
pkill -vto see what would be killed (verbose mode).
Common Examples And Use Cases
Here are practical scenarios where you’ll use these commands:
Scenario 1: A web server has multiple zombie processes. Kill them all with pkill -9 httpd.
Scenario 2: You need to stop all Python scripts running on a server. Use pkill -f "python" but be careful—this kills all Python processes, including your terminal if it’s running Python.
Scenario 3: A user left several background jobs. Kill them with pkill -u username.
Scenario 4: You want to kill all processes except the current shell. Use kill -9 -1 with extreme caution—it kills everything you can kill.
Automating With Scripts
For repetitive tasks, write a simple bash script:
#!/bin/bash
pkill -f "myapp"
echo "All myapp processes killed."
Save it as killmyapp.sh, make it executable with chmod +x killmyapp.sh, and run it whenever needed.
You can also add conditions to avoid killing critical processes:
if pgrep -x "myapp" > /dev/null; then
pkill -x "myapp"
else
echo "No myapp processes running."
fi
Understanding Process Signals
When you kill a process, you’re sending a signal. The most common ones are:
SIGTERM(15): Graceful termination, allows cleanup.SIGKILL(9): Force kill, cannot be ignored.SIGHUP(1): Hang up, often causes reload.SIGINT(2): Interrupt, like pressing Ctrl+C.
Use kill -l to list all available signals.
Why You Should Avoid Killing Processes Blindly
Killing processes without understanding what they do can crash your system. For example, killing systemd or init will halt your machine. Always verify with pgrep or ps first.
If you’re unsure, use pkill -v to see the process names and PIDs that match. This gives you a chance to cancel before damage is done.
Using Htop For Interactive Killing
If you prefer a visual tool, htop lets you select multiple processes with the spacebar and kill them with F9. Install it with sudo apt install htop (Debian/Ubuntu) or sudo yum install htop (RHEL/CentOS).
This is great for beginners who find command-line syntax intimidating.
Killing Processes By Parent Process
To kill a process and all its children, use kill -- -PGID where PGID is the process group ID. Find the PGID with ps -o pid,pgid,comm.
Example: kill -- -1234 kills the process with PID 1234 and all its child processes.
This is useful for stopping entire application trees.
Using Systemd To Kill Services
If you’re managing systemd services, use systemctl stop servicename to stop them gracefully. For stubborn services, use systemctl kill servicename to send a signal.
This is safer than using pkill on system services because systemd tracks dependencies.
Handling Stuck Processes
Sometimes a process won’t die even with SIGKILL. This usually means it’s in an uninterruptible sleep state (D state). In that case, you may need to reboot the system.
Check process states with ps aux | grep ' D'. If you see many D-state processes, your system might have I/O issues.
Logging Killed Processes
For auditing, log which processes you kill:
pkill -f "myapp" && echo "$(date): Killed myapp" >> /var/log/kill.log
This helps you track what was killed and when.
Conclusion
Mastering how to kill multiple processes in Linux saves you time and prevents system clutter. Start with pkill for simple name-based kills, use killall for exact matches, and combine ps with xargs for complex filters. Always test before killing, and prefer SIGTERM over SIGKILL.
With these tools, you can manage any process situation confidently. Practice on non-critical systems first, and soon you’ll be killing processes like a pro.
Frequently Asked Questions
What Is The Difference Between Pkill And Killall?
pkill matches process names by pattern (partial match), while killall requires an exact name. pkill is more flexible but can accidentally kill unintended processes. killall is safer for exact matches.
How Do I Kill Multiple Processes By Name In Linux?
Use pkill processname to kill all processes with that name. For example, pkill firefox kills all Firefox processes. Add -9 for force kill if needed.
Can I Kill Processes By User ID?
Yes, use pkill -u username to kill all processes owned by that user. Be careful—this can log the user out and cause data loss.
What Does Kill -9 Do?
kill -9 sends the SIGKILL signal, which force-kills a process immediately. The process cannot ignore this signal, so it’s used as a last resort when a process doesn’t respond to SIGTERM.
How Do I See What Processes Will Be Killed Before Running The Command?
Use pgrep pattern to list process IDs that match, or use pkill -v pattern to see verbose output of what would be killed. Always test before executing the kill command.