How to Kill a Process on Linux Command Line [5 Best Methods]

Written by

Reviewed by

Last updated: July 12, 2024

Expert verified

SVG Image

TL;DR

To learn how to kill a process on Linux, you can try these methods:

  1. Kill Command: Gracefully terminate a process by sending a SIGTERM signal, allowing it to handle ongoing tasks and clean up before stopping: kill -15 PID.
  2. Killall Command: Forcefully terminate all instances of a process by name, using the -9 (SIGKILL) option with caution to avoid data loss: sudo killall -9 process_name.

Discover different methods to kill a process in Linux in the guide below. Also, learn about common errors that can occur when c the process in Linux.

We’ve all encountered a program that freezes or a process that consumes too many resources, causing our system to slow down. When this happens, knowing how to kill a process in Linux can be a lifesaver. In this post, I’ll show you various methods to kill processes, explain when to use them, and troubleshoot common errors. You’ll also learn about important signals and how to locate processes efficiently. Let’s explore and take control of your Linux processes!

What are Signals?

In Linux, signals are a way for the operating system to communicate with processes. They are used to tell processes to stop, continue, or take specific actions. Think of signals as messages that the system sends to processes to control their behavior.

Common Signals Used to Kill Processes

  • SIGTERM (15) – Terminate a Process Gracefully: The purpose of SIGTERM is to ask a process to stop running. It sends a polite request to the process, giving it a chance to clean up and close down properly. Most programs are designed to handle this signal and will terminate safely.
  • SIGKILL (9) – Forcefully Kill a Process: The purpose of SIGKILL is to force a process to stop immediately. It sends a strong command that the process cannot ignore, causing the process to stop right away without any cleanup. Use this only when necessary, as it can cause data loss or corruption.
  • SIGHUP (1) – Hang Up Signal: The purpose of SIGHUP was originally to signal that a terminal connection was lost. It is often used to tell a process to restart or reload its configuration. Many daemons (background processes) will reload their settings when they receive this signal.

When to Kill a Process

Killing a process means forcing it to stop running. You should only do this when necessary, as it can disrupt the system or cause data loss. Here are common situations when you might need to kill a process:

  1. Unresponsive Programs: If a program freezes and stops responding to input, you might need to kill it to regain control of your system.
  2. High CPU or Memory Usage: Sometimes, a process uses too much CPU or memory, slowing down your computer. Killing such a process can free up resources and improve performance. Read this guide for detailed methods to check CPU frequency.
  3. System Stability: If a process is causing the system to become unstable or crash, killing it can help stabilize the system.
  4. Zombie Processes: These are processes that have completed execution but still linger in the process table. Killing them cleans up the system.
  5. Testing and Development: During software development, you might need to kill processes frequently to restart them with new changes.

How to Locate a Process

Finding a process in Linux involves identifying its Process ID (PID) or name.The ps command is a powerful tool for listing and inspecting running processes on your Linux system. It provides detailed information about each process, including its Process ID (PID), user, CPU usage, memory usage, and the command that started it. Follow these steps to effectively use the ps command:

  1. Launch your terminal application. To see a comprehensive list of all running processes, type the following command and press Enter:
ps aux

This command will display detailed information about each process, including the PID, user, CPU usage, memory usage, and the command that initiated the process.

viewing list of all running processes
  1. If you are looking for a specific process, you can filter the list using the grep command. For example, to find processes related to a program named firefox, you can use:
ps aux | grep firefox

This command will show only the processes that contain the word “firefox” in their details, making it easier to locate the exact process you need.

listing a specific process

The output will be a table with several columns. Here’s a quick guide to the main columns:

  • USER: The user who started the process.
  • PID: The Process ID, which is a unique number assigned to each process.
  • %CPU: The percentage of the CPU that the process is using.
  • %MEM: The percentage of memory that the process is using.
  • COMMAND: The command that was used to start the process.

How to Kill a Process on Linux

To kill process Linux, you can use several commands depending on the situation. The kill command allows you to gracefully terminate a process by sending a SIGTERM signal, kill -15 PID. If you need to terminate all instances of a process by name, use the killall command, with the -9 (SIGKILL) option for a forceful termination, sudo killall -9 process_name. These commands help you manage and control processes efficiently on your Linux system.

That was the quick answer. Here are the detailed steps for five different methods to Linux terminate process:

1. Kill Command

The kill command Linux is an essential tool for gracefully terminating processes in a Linux environment. This method is best used when you want to end a process smoothly, giving it a chance to handle any ongoing tasks and release resources gracefully. Follow these steps:

Identify the process to be terminated:

  1. Open a Terminal window.
opening terminal 34
  1. Use the ps command to list all processes currently running on the system. Note the PID (Process ID) of the process you want to terminate.
listing active processes
  1. Execute the following command:
kill -15 PID

Replace PID with the actual Process ID of the target process.

  1. This sends a SIGTERM signal to the process, allowing it to clean up before termination. Check if the process has been terminated by using the ps command.
killing specific process

2. Killall Command

The killall command provides a forceful approach to terminate processes based on their names. It is useful when you need to deal with multiple instances of the same process quickly. Here is the step-by-step guide to it:

  1. Launch the Terminal and identify the process name to be terminated:
  2. Run the following command (with sudo, if necessary):
sudo killall -9 process_name

Replace process_name with the name of the target process.

  1. Be cautious when using -9 (SIGKILL) as it terminates the process immediately without allowing cleanup. Check if all instances of the process have been terminated using the ps command or monitoring tools.
killing all instances of a process

3. pkill Command

The pkill command is a versatile tool that allows you to target processes based on specific attributes such as process names or user ownership. It provides a more granular approach to process termination and is best used when you want to terminate specific processes efficiently and in a controlled manner. Follow these steps:

  1. Identify the processes to be terminated based on specific attributes:
  2. Access the command window and determine the attributes (e.g., process name, user) of the target processes. Enter the following command:
pkill -u username process_name

Replace username with the name of the user owning the target processes and replace process_name with the name of the target process.

killing process based on attributes

4. killall5 Command

The killall5 command is a powerful tool to initiate system-level termination by sending a SIGTERM signal to all non-essential processes. It should be used with utmost care, primarily during system shutdown or in critical situations, to avoid disrupting essential system operations and ensure a smooth shutdown process. Follow these steps:

  1. Open the command prompt and use the killall5 command to initiate system-level termination.
  2. Execute the following command (with sudo, if necessary):
sudo killall5 -15
  1. This sends a SIGTERM signal to all non-essential processes, allowing them to clean up before termination.
terminating processes on system level

5. xkill Command

When graphical applications become unresponsive and fail to close through conventional means, the xkill command comes to the rescue. This method is ideal for dealing with unresponsive applications on the graphical user interface (GUI) without resorting to Terminal-based solutions. Follow these steps:

  1. Enter your Terminal window and execute the following command:
xkill
  1. The cursor will change to an X symbol. Click on the unresponsive graphical window to force-close it.
killing unresponsive window

5 Common Errors When Killing a Process in Linux

Killing processes in Linux can be a powerful and necessary action, but it comes with its share of potential pitfalls. Understanding these common errors will help you avoid unintended consequences and ensure smooth system management. Here are the five most common errors encountered when terminating processes in Linux.

  • Using SIGKILL (kill -9) Unnecessarily: Using SIGKILL should be a last resort when other methods fail. SIGKILL forces the process to stop immediately, causing potential data corruption. Always try SIGTERM (kill -15) first to allow the process to terminate gracefully. Remember, forcefully killing a process should be avoided unless absolutely necessary.
  • Terminating Essential System Processes: Terminating critical system processes can cause system crashes or data loss. Ensure the process is non-essential before killing it. Verify its purpose and impact to avoid disrupting vital operations. Remember, mistakes in terminating essential processes can bring the entire system down.
  • Insufficient Permissions for Process Termination: Some processes require root privileges to terminate. Attempting to kill such processes without the necessary permissions results in “Permission Denied” errors. Always check your privileges before attempting to terminate processes. Ensure you have the appropriate permissions to avoid these errors.
  • Improper Process Identification: Incorrectly identifying the target process by PID or name can lead to unintended terminations. Double-check the process details before using kill commands to avoid accidentally terminating critical or unrelated processes. Verify the process details accurately to prevent disruptions.
  • 🧟Neglecting Zombie Processes: Zombie processes are not actively running but still occupy system resources. Neglecting them can lead to resource wastage and performance issues. Identify and terminate the parent process of zombies to clean up these idle resources. Handle zombie processes promptly to maintain system efficiency.

Linux Kill Process: Wrapping Up

In this article, I have explored various methods to kill a process Linux, including using kill, killall, pkill, killall5, and xkill commands. I also showed you how to locate processes with the ps command and discussed when and why you might need to kill a process, along with troubleshooting common errors for effective system management.

If you want to deepen your Linux knowledge, check out these related articles:

Frequently Asked Questions

Can I kill a process running under another user’s account?

Killing a process owned by another user usually requires root privileges. You can use the sudo command with kill or pkill to terminate processes belonging to other users. However, exercise caution as this action can impact system stability. Granting root access to process termination should be done judiciously to avoid unintended consequences and potential security risks.

What is the difference between SIGTERM and SIGKILL signals, and when should I use them?

SIGTERM allows processes to perform cleanup tasks before termination, providing a graceful exit. In contrast, SIGKILL forcefully terminates the process without any opportunity for cleanup. Use SIGTERM when you want the process to handle termination tasks before closing, especially for critical processes. Reserve SIGKILL as a last resort if a process becomes unresponsive to SIGTERM or poses a serious threat to the system’s stability.

Can killing critical system processes cause data corruption or system instability?

Yes, terminating critical system processes can lead to data corruption, system instability, and even system crashes. Critical processes are essential for the proper functioning of the operating system and other applications. Terminating them abruptly can disrupt critical services and cause unintended consequences. Exercise extreme caution when terminating processes and avoid killing essential system services unless it’s absolutely necessary for system troubleshooting or recovery.

How can I prioritize process termination to prevent essential services from getting killed?

Prioritize process termination by specifying the appropriate signal. Use SIGTERM (kill -15) for graceful termination, giving the process a chance to clean up before exiting. This allows essential services to shut down properly and prevent data loss or corruption. Only resort to SIGKILL (kill -9) as a last resort when a process becomes unresponsive to SIGTERM or poses an immediate threat to the system’s stability. Properly managing process termination ensures a smooth and stable system operation.

How do I stop a running process in Linux?

To stop a running process in Linux, you can use the kill -STOP command followed by the process ID (PID). This command pauses the process, allowing you to resume it later with a different command if needed. For example, kill -STOP 1234 will stop the process with PID 1234.

How can I terminate a process gracefully in Linux?

To terminate a process gracefully, you can use the kill -TERM command followed by the process ID (PID). This sends a termination signal to the process, allowing it to perform cleanup before shutting down. For instance, kill -TERM 1234 will terminate the process with PID 1234 gracefully.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to List Docker Containers in Linux [3 Effective Methods]

Next Post

How to Install GNOME Disks Utility on Ubuntu and Configure It [3 Simple Methods]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next