Killing Processes in Linux: A Complete Guide

Need to kill a process in Linux? Here are a few ways to do it and why you’d want to

Author: Jeremy Morgan
Published: January 3, 2024


Processes are the background operations that make up an operating system’s functionality. They can include anything from web servers and databases to utility programs, scripts, and even Python or Node.js applications. In Linux, processes are managed by the kernel and can be easily identified with their associated PID (Process ID).

“How to Kill a Process in Linux”

But sometimes, they need to die. If something is hanging up or causing a problem you need to terminate the process. Let’s look at a few more reasons.

Why We Want to Do It

There are several reasons why you’d want to kill a process in Linux:

  1. Prevent an application from consuming all available system resources.
  2. Terminate a running process that is causing errors or malfunctioning.
  3. Improve system stability and overall performance by terminating long-running processes.
  4. Avoid exposing sensitive data or unauthorized access with specific processes.
  5. Maintain security by stopping any potentially malicious applications.

How to Kill a Process in Linux

Here are several ways to kill a process in Linux:

Method 1: Using the ‘kill’ Command

The most basic way to stop a process is through the ‘kill’ command, which takes two arguments - the PID of the process you want to terminate and an optional signal. Signals provide additional control over how the process is terminated. The syntax for this method is:

sudo kill -9 [PID]

As an example, we have a process named ‘fundmanager’. I can find it by typing in ps -aux:

“How to Kill a Process in Linux”

And as I can see here my fundmanager is running under PID 1901. So I type in

sudo kill -9 1901

If you don’t see anything on the screen afterward, it was successful:

“How to Kill a Process in Linux”

And if we re run ps -aux we can see the process has restarted with an ID of 1917.

“How to Kill a Process in Linux”

Method 2: Using the ‘pkill’ Command

The ‘pkill’ command is similar to ‘kill’, but instead of specifying a PID, it lists all processes that match specific criteria. You can include various operators like ‘==’, ‘!=’, ‘<’, ‘>’, ‘%', and ‘=’ to refine your search for matching processes.

For example: To kill all instances of an application named “fundmanager”, use the command sudo pkill -f fundmanager.

Method 3: Using Ctrl+C (Keyboard Interrupt)

The most intuitive way to stop a process is through keyboard interrupt or ‘Ctrl+C’. This command is universally recognized by Linux and can be used in various terminal emulators. If you are in the process at the prompt, Ctrl+C will usually get you out of it.

Things You Should Know:

  • Always make sure you know what processes are running before terminating them. Unintended termination of essential services might cause system instability or data loss.
  • The ‘sudo’ prefix is optional but recommended when using commands like ‘kill’ or ‘pkill’, as it grants root privileges, allowing access to terminate any process on the system regardless of its current user’s permissions.
  • If you’re unsure about which PID corresponds to a specific running application, consider using ‘pgrep’ command in conjunction with ‘kill’ or ‘pkill’. For example: sudo pgrep -f fundmanager will provide a list of all processes matching “fundmanager”.

“How to Kill a Process in Linux”

  • Be cautious when using the ‘SIGKILL’ signal, as it results in permanent deletion and cannot be undone. Make sure to confirm if you’re deleting important data or system services before using this method.

  • Monitor your systems after performing process termination for any unintended consequences on system stability or performance. If issues arise, revert any changes made by reinstating the previously terminated processes.

Summary

Killing a process in Linux is an essential task to maintain system stability and security while protecting sensitive data and preventing system crashes caused by malfunctioning applications. Be aware of potential side effects and take appropriate steps to ensure your actions have minimal impact on your overall system performance.


Questions or Comments? Yell at me!

- Jeremy