sodcalculator

10 Tech How-To

Kill Process by Name in Linux: A Practical Guide

Guide 10-20423 · Updated · Step by step

This guide shows you how to terminate a running process by its name in Linux, which is faster and more reliable than hunting for its PID. You’ll use commands like pkill, killall, and pgrep to find and stop processes without needing to know their numeric IDs. No special tools are required—everything you need is already in a standard Linux terminal.

What You’ll Need

  • A Linux system with a terminal (any distribution works).
  • Basic permission to run commands—you’ll need sudo for processes owned by other users or system services.
  • Knowledge of the exact process name you want to kill (use ps aux | grep to find it if unsure).

Step-by-Step

  1. Identify the process name. Run ps aux | grep -i <name> to list processes whose command line contains that name. For example, ps aux | grep -i firefox shows all Firefox-related processes. Note the exact name in the output—it’s usually the first field after the user.
  2. Check the process name with pgrep. Use pgrep -l <name> to list PIDs and names that match. This confirms the exact string you’ll use. For instance, pgrep -l firefox might output 1234 firefox and 5678 firefox-esr.
  3. Kill a single process by name. Run pkill <name> to send the default SIGTERM signal to all processes with that name. Example: pkill firefox. This asks the process to exit gracefully, allowing it to save data.
  4. Kill all matching processes. If multiple processes share the name (like chrome or node), pkill already targets all of them. For a more explicit approach, use killall <name>—it also kills every process with that exact name.
  5. Force kill if the process ignores the request. If the process doesn’t exit after a few seconds, send SIGKILL with pkill -9 <name> or killall -9 <name>. This forcibly terminates it, but unsaved data will be lost.
  6. Kill only processes owned by you. By default, pkill and killall affect only your own processes. To kill another user’s process, you need root: sudo pkill <name>.
  7. Use exact name matching to avoid accidents. Add -x to pkill or killall to match the process name exactly, not partially. For example, pkill -x firefox won’t kill firefox-esr or firefox-bin.
  8. Verify the process is gone. Run pgrep -l <name> again. If it returns nothing, the process has been terminated successfully.

Common Mistakes

  • Using a partial name without -x. pkill fire will kill any process whose name contains “fire”, including firefox and firewalld. Always use -x for exact matches.
  • Forgetting that pkill matches command lines, not just names. By default, pkill matches against the full command line. A process like python script.py will be killed by pkill python but also by pkill script.py if you’re not careful.
  • Killing the wrong user’s process. Without sudo, you can’t kill processes owned by other users, but with sudo you might accidentally kill a system service. Double-check the PID with pgrep first.
  • Using -9 immediately. SIGKILL doesn’t allow the process to clean up files or release resources. Always try SIGTERM first.

Tips

  • Use pkill -i for case-insensitive matching. This is handy when you’re not sure about capitalization, e.g., pkill -i -x chrome.
  • Preview with pgrep before killing. Run pgrep -a <name> to see the full command lines of matching processes, so you know exactly what you’re about to kill.
  • Kill by pattern with pkill -f. This matches against the entire command line, useful for scripts like pkill -f 'python server.py'.
  • Use killall -r for regular expression matching. For example, killall -r '^java' kills all processes starting with “java”.
  • Check the signal list. Run kill -l to see all available signals. SIGTERM (15) is the default, SIGKILL (9) is the hard stop.

Troubleshooting

“pkill: no process found” — The name doesn’t match any running process. Check the spelling and use ps aux | grep -i <name> to see the actual process names. Remember that some processes have a different name than the executable (e.g., chromium vs chrome).

Permission denied. You’re trying to kill a process owned by another user or a system process. Use sudo before the command, but be cautious—killing system processes can destabilize your system.

Process keeps respawning. Some services are managed by systemd or a supervisor that restarts them automatically. Stop the service first with systemctl stop <service> or use systemctl --user stop for user services.

Process still running after pkill. It may be ignoring SIGTERM. Wait a few seconds, then use pkill -9. If it still persists, check if it’s a zombie (shown as Z in ps status)—zombies can’t be killed and will be cleaned up by their parent.

Accidentally killed the wrong process. If you terminated a critical system process, restart it with systemctl start <service> or reboot the system. For user applications, just relaunch them.

Frequently Asked Questions

What is the difference between pkill and killall? Both kill processes by name. pkill is more flexible—it supports partial matches, regular expressions, and matching against command lines. killall is simpler and by default requires an exact name match.

Can I kill a process without knowing its exact name? Yes, use pkill -f to match against the full command line. For example, pkill -f 'python.*server' kills any Python process running a server script.

How do I kill all processes of a specific user? Use pkill -u <username> to kill all processes owned by that user. Be careful—this will log them out and close their sessions.

What does SIGTERM vs SIGKILL mean? SIGTERM (signal 15) is a polite request to terminate, allowing the process to save data and clean up. SIGKILL (signal 9) forces immediate termination, which can cause data loss but guarantees the process dies.

Is it safe to kill a process by name if multiple instances are running? Yes, but be aware that pkill and killall will terminate all instances. If you only want to kill one, use pgrep to find its PID and then kill <PID>.

Can I undo a kill? No, once a process is terminated, it cannot be resumed. You can only restart it if it’s a service or you have the command to launch it again.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →