ATB Team

How to Kill a Process on Linux

So, you want to kill a process on Linux? No worries, let’s break it down. There are a few commands you can use for this, like kill, killall, pkill, and even xkill for GUI processes.

First, to kill a process, you’ll need to find its process ID (PID). You can use commands like ps, top, or pgrep to locate it. For example, running ps aux | grep process_name will help you find the PID of a specific process. Once you have the PID, you can use the kill command to send a signal to the process. The default signal is SIGTERM (15), which tries to close the process gracefully. If that doesn’t work, you can use SIGKILL (9), which forces the process to quit immediately. I’d recommend trying SIGTERM first and then moving to SIGKILL if needed.

Alternatively, if you don’t want to mess with finding a PID, killall is a great option because it lets you use the process name directly instead of the PID. Similarly, pkill can be used to match patterns, so you can kill multiple instances of a process at once. After sending the kill signal, you might want to check if the process is still running. You can do that by using ps or pgrep again to verify.

A few things to watch out for: If you don’t have permission to kill a process, you might need to run the command with sudo. Also, be aware of zombie processes that might not die easily. And always double-check the PID to avoid accidentally killing the wrong process. If you’re using a desktop environment, you can also kill processes with GUI tools like System Monitor, which is a bit more visual if you’re not into the command line.

Here’s a step-by-step guide to use commands like kill, pkill, or killall to terminate it by PID (Process ID) or name :-

1. Find the Process ID (PID)

Using ps or pgrep

Example:

Using pgrep

Using top or htop

  • Run top or htop, navigate to the process, and note the PID.

2. Terminate the Process

Kill by PID

Example:

Kill by Name

Example:

3. Verify the Process is Killed

Key Signals

SignalValuePurpose
SIGTERM15Graceful termination (default for kill).
SIGKILL9Forceful termination (unstoppable).
SIGHUP1Reload/restart (e.g., kill -1 PID).

Advanced Methods

Kill Processes by Port

Kill User-Owned Processes

Kill GUI Applications

Use xkill (if running a desktop environment):

Troubleshooting

  • Permission Denied: Use sudo if the process is owned by another user:
  • Zombie Processes: These can’t be killed (already dead). Reboot to clear them.
  • Process Respawns: Some processes auto-restart (e.g., system services). Use:

Example Workflow

  1. Find the PID of a frozen app:
  1. Terminate it gracefully:
  1. If it persists, force-kill:
  1. Verify:

Important Notes

  • Use SIGKILL (-9) sparingly—it doesn’t allow cleanup and can corrupt data.
  • Prefer SIGTERM first to let the process exit properly.
  • Avoid killing critical system processes (e.g., systemd, sshd).

Summary Table

CommandUse Case
kill PIDGraceful termination (SIGTERM).
kill -9 PIDForce-kill (SIGKILL).
pkill "name"Kill by process name.
killall "name"Kill all instances of a process.
xkillKill GUI apps interactively.

That’s all.

Leave a Comment

Table Of Content