Skip to main content

Command Palette

Search for a command to run...

How Do I Kill a Process in Linux

Updated
7 min read

Killing a process in Linux is a common task you might need to do when a program freezes or behaves unexpectedly. If you’re new to Linux or just want to understand how to manage processes better, you’re in the right place. I’ll guide you through the easiest and most effective ways to stop a process safely.

You don’t need to be a Linux expert to handle this. Whether you want to kill a process by its name or by its process ID (PID), I’ll show you step-by-step commands and tips. By the end, you’ll feel confident managing processes on your Linux system.

Understanding Processes in Linux

Every program or application running on your Linux system is called a process. Each process has a unique number called a Process ID (PID). When you want to stop a program, you need to tell the system which process to kill.

Processes can be in different states, like running, sleeping, or stopped. Sometimes, a process might not respond, and you need to force it to stop. That’s where killing a process comes in handy.

Here are some key points about processes:

  • Each process has a PID.
  • Processes can be user-initiated or system processes.
  • Killing a process stops it immediately.
  • Some processes require special permissions to kill.

Knowing this helps you understand why you need to identify the right process before killing it.

How to Find the Process ID (PID)

Before killing a process, you need to find its PID. There are several ways to do this:

Using the ps Command

The ps command lists running processes. You can filter it to find the process you want.

ps aux | grep process_name
  • ps aux shows all running processes.
  • grep process_name filters the list to show only processes matching the name.

For example, to find the PID of Firefox:

ps aux | grep firefox

This will show lines with Firefox processes and their PIDs in the second column.

Using the pidof Command

pidof gives you the PID of a running program by name.

pidof process_name

For example:

pidof firefox

This returns the PID(s) of Firefox processes.

Using the top or htop Command

top and htop are interactive tools that show running processes in real-time.

  • Run top and look for the process name.
  • The PID is shown in the first column.
  • You can press k in top to kill a process by entering its PID.

htop is more user-friendly and allows you to scroll and select processes easily.

Basic Ways to Kill a Process in Linux

Once you have the PID, you can kill the process using several commands.

Using the kill Command

The kill command sends signals to processes. The most common signal is SIGTERM (signal 15), which asks the process to terminate gracefully.

kill PID

Replace PID with the actual process ID.

If the process does not stop, you can force kill it with SIGKILL (signal 9):

kill -9 PID

SIGKILL forces the process to stop immediately without cleanup.

Using the pkill Command

pkill kills processes by name, so you don’t need to find the PID first.

pkill process_name

For example:

pkill firefox

This sends the default SIGTERM signal to all Firefox processes.

To force kill:

pkill -9 process_name

Using the killall Command

killall kills all processes with a given name.

killall process_name

Like pkill, you can add -9 to force kill:

killall -9 process_name

Using top to Kill a Process

In top, press k, then enter the PID of the process you want to kill. You can also specify the signal number (default is 15).

This method is useful if you want to see system resource usage and kill processes interactively.

Understanding Signals When Killing Processes

Linux uses signals to communicate with processes. When you kill a process, you send it a signal. Here are some common signals:

Signal NameNumberDescription
SIGTERM15Graceful termination request
SIGKILL9Force kill, cannot be ignored
SIGINT2Interrupt from keyboard (Ctrl+C)
SIGHUP1Hangup detected, often reload config

Usually, you start with SIGTERM to allow the process to close properly. If it ignores this, use SIGKILL to force it.

Killing Processes as a Non-Root User

You can only kill processes that you own unless you have root privileges. If you try to kill a system or another user’s process, you’ll get a permission error.

To kill system processes, use sudo:

sudo kill PID

Or

sudo pkill process_name

Be careful when killing system processes, as it may affect system stability.

Using Graphical Tools to Kill Processes

If you prefer not to use the command line, many Linux desktop environments have graphical tools:

  • System Monitor (GNOME): Shows running processes, allows you to right-click and kill.
  • KSysGuard (KDE): Similar to System Monitor, with process management features.
  • Xfce Task Manager: Lightweight tool for managing processes.

These tools provide an easy way to find and kill processes without typing commands.

Tips for Safely Killing Processes

Killing processes can sometimes cause data loss or system issues. Here are some tips to do it safely:

  • Always try to close the program normally first.
  • Use kill without -9 to allow cleanup.
  • Avoid killing system-critical processes.
  • Check what the process is doing before killing it.
  • Use top or htop to monitor process behavior.

Troubleshooting When a Process Won’t Kill

Sometimes, a process refuses to die even after kill -9. This can happen if:

  • The process is stuck in kernel mode.
  • It’s a zombie process (already dead but not cleaned up).
  • There is a hardware or driver issue.

In such cases:

  • Rebooting the system might be necessary.
  • Check system logs for errors.
  • Investigate if a driver or hardware problem is causing the hang.

Summary Table of Commands to Kill Processes

CommandDescriptionExample
kill PIDSend SIGTERM to PIDkill 1234
kill -9 PIDForce kill PID with SIGKILLkill -9 1234
pkill process_nameKill by process name (SIGTERM)pkill firefox
pkill -9 process_nameForce kill by name (SIGKILL)pkill -9 firefox
killall process_nameKill all processes by namekillall firefox
killall -9 process_nameForce kill all by namekillall -9 firefox
topInteractive process viewer and killerRun top, press k

Conclusion

Now you know how to kill a process in Linux using various commands and tools. Whether you prefer command-line methods like kill, pkill, or interactive tools like top, you have options to manage processes effectively. Remember to identify the right process and use signals carefully to avoid unwanted issues.

Killing processes is a useful skill that helps keep your Linux system running smoothly. With practice, you’ll handle frozen or misbehaving programs confidently and safely.

FAQs

How do I find the PID of a process in Linux?

You can use ps aux | grep process_name, pidof process_name, or interactive tools like top or htop to find the PID of a running process.

What is the difference between kill and pkill?

kill requires a PID to stop a process, while pkill kills processes by name, making it easier when you don’t know the PID.

Can I kill system processes as a regular user?

No, you need root privileges to kill system or other users’ processes. Use sudo before the kill command to gain permission.

What does kill -9 do?

kill -9 sends the SIGKILL signal, which forces a process to stop immediately without cleanup. Use it only if normal kill doesn’t work.

Is it safe to kill any process?

Not always. Killing critical system processes can cause instability. Always try to close programs normally and avoid killing unknown or system processes.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts