The two most common ways to terminate a process in Linux at the CLI are using the `kill` command and the `pkill` command. Both are essential tools for managing running programs, but they work in slightly different ways. Understanding these methods will help you control your system effectively, whether you’re dealing with a frozen application or a runaway service.
In this guide, we’ll walk through each method step by step. You’ll learn how to use `kill` with process IDs and `pkill` with process names. We’ll also cover signals, common use cases, and practical tips to avoid mistakes.
What Are The Two Most Common Ways To Terminate A Process In Linux At The Cli
The `kill` command sends a signal to a process based on its process ID (PID). The `pkill` command does the same but uses the process name instead. Both are built into every Linux distribution, so you don’t need to install anything extra.
Let’s break down each method in detail. We’ll start with `kill`, then move to `pkill`, and finish with comparisons and best practices.
Understanding Linux Process Termination Basics
Before diving into commands, it helps to know how processes work. Every running program in Linux is a process with a unique PID. When you want to stop one, you send a signal. Signals are like messages that tell the process what to do.
The most common signals are:
- SIGTERM (15) – Politely asks the process to terminate. It can clean up resources.
- SIGKILL (9) – Forcefully kills the process. It cannot be ignored or handled.
- SIGHUP (1) – Often used to reload configuration files.
- SIGINT (2) – Interrupts the process, like pressing Ctrl+C.
By default, both `kill` and `pkill` send SIGTERM. If the process doesn’t respond, you can escalate to SIGKILL.
Method 1: Using The Kill Command
The `kill` command is the classic way to terminate processes. It requires you to know the PID of the target process. You can find PIDs using commands like `ps`, `top`, or `pgrep`.
Here’s the basic syntax:
kill [signal] PID
For example, to terminate a process with PID 1234 using SIGTERM:
kill 1234
To force kill with SIGKILL:
kill -9 1234
You can also specify the signal by name:
kill -SIGTERM 1234
Now, let’s walk through a real-world example step by step.
Step 1: Find The Process ID
Suppose Firefox is frozen. First, find its PID:
ps aux | grep firefox
This lists all processes with “firefox” in the name. Look for the PID in the second column. Alternatively, use `pgrep`:
pgrep firefox
This returns only the PID numbers.
Step 2: Send A Termination Signal
Once you have the PID, say 5678, send SIGTERM:
kill 5678
If Firefox doesn’t close within a few seconds, force kill it:
kill -9 5678
That should stop it immediately.
Step 3: Verify The Process Is Gone
Check if the process still exists:
ps aux | grep 5678
If no output appears, the process is terminated.
The `kill` command is precise because you target a specific PID. However, it can be tedious if you have multiple processes with similar names.
Method 2: Using The Pkill Command
The `pkill` command simplifies termination by matching process names instead of PIDs. It’s part of the `procps-ng` package and is available on all major distributions.
Basic syntax:
pkill [options] pattern
For example, to kill all processes named “firefox”:
pkill firefox
To force kill:
pkill -9 firefox
You can also use signals by name:
pkill -SIGTERM firefox
Let’s see a practical example.
Step 1: Identify The Process Name
You don’t need the PID, just the name. Use `ps` to confirm the exact name:
ps aux | grep -i firefox
The process name is usually the command that started it, like “firefox” or “chrome”.
Step 2: Send A Signal By Name
To terminate all Firefox processes:
pkill firefox
If you want to be more specific, use the `-f` flag to match the full command line:
pkill -f "firefox -new-window"
This kills only Firefox instances started with the `-new-window` option.
Step 3: Force Kill If Needed
If Firefox ignores SIGTERM:
pkill -9 firefox
This sends SIGKILL to all matching processes.
One advantage of `pkill` is that it can terminate multiple processes at once. For example, if you have several Firefox tabs running as separate processes, `pkill firefox` kills them all.
Comparing Kill And Pkill
Both commands have strengths and weaknesses. Here’s a quick comparison:
| Feature | kill | pkill |
|---|---|---|
| Requires PID | Yes | No |
| Matches by name | No | Yes |
| Kills multiple processes | No (one at a time) | Yes |
| Precision | High (targets one PID) | Medium (matches pattern) |
| Ease of use | Requires finding PID | Simple name matching |
Use `kill` when you need to target a specific process and avoid affecting others. Use `pkill` when you want to terminate all processes with a given name quickly.
Common Signals And Their Uses
Knowing which signal to send is crucial. Here are the most useful ones:
- SIGTERM (15) – Default. Graceful termination.
- SIGKILL (9) – Force kill. Use only if SIGTERM fails.
- SIGHUP (1) – Reload configuration. Often used for daemons.
- SIGINT (2) – Interrupt. Same as Ctrl+C.
- SIGSTOP (19) – Pause the process (cannot be ignored).
- SIGCONT (18) – Resume a paused process.
You can list all signals with:
kill -l
For `pkill`, use the same signal numbers or names.
Practical Tips For Safe Termination
Terminating the wrong process can crash your system or lose data. Follow these tips:
- Always try SIGTERM first. It gives the process a chance to save data.
- Use `pgrep` to preview matches. Before running `pkill`, test with `pgrep` to see which PIDs will be affected.
- Be careful with `pkill -f`. It matches the full command line, which can include arguments. This might kill unintended processes.
- Don’t kill system processes. Processes like `init` (PID 1) or `systemd` are essential. Killing them can freeze your system.
- Use `killall` with caution. Some distributions have `killall` which works like `pkill`, but it’s not always available.
Here’s a safe workflow for terminating a stubborn process:
- Find the PID with `ps aux | grep processname`.
- Send SIGTERM: `kill PID`.
- Wait 5-10 seconds.
- If still running, send SIGKILL: `kill -9 PID`.
Alternatively, use `pkill` with the same approach:
- Preview with `pgrep processname`.
- Send SIGTERM: `pkill processname`.
- Wait, then force kill: `pkill -9 processname`.
Advanced Usage: Combining Commands
You can combine `kill` with other tools for more control. For example, to kill all processes owned by a user:
kill -9 $(pgrep -u username)
Or using `pkill`:
pkill -u username
You can also kill processes by group ID (GID) or session ID. These are less common but useful in scripting.
Another trick is to use `xargs` with `kill`:
ps aux | grep firefox | awk '{print $2}' | xargs kill -9
This finds all Firefox PIDs and kills them. However, `pkill` does the same thing more simply.
Common Mistakes And How To Avoid Them
Even experienced users make errors. Here are typical pitfalls:
- Killing the wrong PID. Double-check with `ps` before running `kill`.
- Using SIGKILL too soon. This can cause data loss. Always try SIGTERM first.
- Forgetting to escape patterns. If your process name has special characters, use quotes: `pkill “my app”`.
- Assuming `pkill` matches exactly. By default, `pkill` matches substrings. For example, `pkill fire` would kill “firefox” and “firewalld”. Use `-x` for exact match.
- Not checking permissions. You can only kill processes you own, unless you’re root. Use `sudo` if needed.
To avoid these, always preview with `pgrep` and use the `-x` flag for exact name matching with `pkill`.
When To Use Each Method
Here are scenarios where one method shines over the other:
- Use `kill` when:
- You know the exact PID.
- You want to target one specific instance.
- You’re writing scripts that need precise control.
- Use `pkill` when:
- You don’t know the PID.
- You want to kill all instances of a program.
- You need to kill processes by name quickly.
In practice, many users rely on `pkill` for everyday tasks because it’s faster. But `kill` remains essential for advanced scenarios.
Alternatives To Kill And Pkill
While `kill` and `pkill` are the most common, there are other tools:
- killall – Similar to `pkill` but uses exact name matching by default. Available on some systems.
- xkill – A graphical tool that lets you click on a window to kill its process. Run `xkill` in a terminal, then click the frozen window.
- htop – An interactive process viewer where you can select and kill processes with function keys.
- systemctl – For managing systemd services. Use `systemctl stop servicename` to terminate services.
These are useful but less direct than `kill` and `pkill` for general process termination.
Troubleshooting Stubborn Processes
Sometimes a process won’t die even with SIGKILL. This can happen if the process is in an uninterruptible sleep state (D state) or is a zombie (Z state).
For zombie processes, you can’t kill them directly. Instead, you must kill their parent process. Use `ps -o ppid= -p ZOMBIE_PID` to find the parent, then kill that.
For processes in D state, they are waiting for I/O. You may need to wait for the I/O to complete or reboot the system.
If you suspect a process is stuck, check its state with:
ps -o state,pid,cmd -p PID
If the state is “D” or “Z”, standard termination won’t work.
Automating Termination With Scripts
You can write simple scripts to terminate processes based on conditions. For example, to kill all Firefox processes if they exceed 1 GB of memory:
#!/bin/bash
for pid in $(pgrep firefox); do
mem=$(ps -o rss= -p $pid)
if [ $mem -gt 1048576 ]; then
kill $pid
fi
done
This script checks each Firefox process’s resident set size (RSS) and kills those over 1 GB. You can adapt it for other programs.
For cron jobs, you might want to kill a process that runs too long. Use `timeout` command instead:
timeout 30 command
This runs `command` and kills it after 30 seconds.
Security Considerations
Terminating processes can have security implications. For example, killing a security daemon could leave your system vulnerable. Always verify what a process does before killing it.
Also, be aware that `pkill` can match unintended processes if the pattern is too broad. Use the `-x` flag for exact matches to avoid accidents.
On multi-user systems, only root can kill processes owned by other users. If you’re not root, you’ll get a “Operation not permitted” error.
Frequently Asked Questions
What Is The Difference Between Kill And Pkill?
The main difference is that `kill` requires a process ID (PID), while `pkill` uses the process name. `kill` targets one process at a time, whereas `pkill` can kill multiple processes matching a pattern.
Can I Use Kill To Terminate Multiple Processes At Once?
Yes, you can pass multiple PIDs to `kill` like this: `kill 1234 5678 9012`. But you must know each PID. `pkill` is easier for bulk termination by name.
What Signal Should I Use First?
Always try SIGTERM (signal 15) first. It allows the process to clean up. If that fails, use SIGKILL (signal 9) as a last resort.
How Do I Find The PID Of A Process?
Use `ps aux | grep processname` or `pgrep processname`. The PID is the second column in `ps` output.
Is Pkill Available On All Linux Distributions?
Yes, `pkill` is part of the `procps-ng` package, which is installed by default on