How to Kill a Process in Linux

When errors occur in Linux or the system misbehaves, it’s important to understand the various ways to kill processes — here’s how to do it from the Linux command line.

Written by Trung Thanh Le
developer writing linux code
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | Aug 09, 2024

In Linux systems, you may encounter a misbehaving or unresponsive process that requires immediate termination. In such cases, having an understanding of how to identify, assess and kill processes in Linux is invaluable. Here’s what commands to use to locate and kill a Linux process from the command line.

How to Kill a Process in Linux With kill Command

To kill a Linux process using the kill command, use the syntax:

kill [signal] [PID]

In this code:

  • [signal] is the specified signal you want to use to terminate the process. -15 is the default signal sent when no signal is specified by the user.
  • [PID] is the process ID (PID) of the process you want to terminate.
A tutorial on how to kill process in Linux. | Video: HOWTECH

How to Find a Linux Process

Before killing a Linux process, you have to first locate it and identify its Process ID (PID). This can be done in a few different ways.

1. Locate a Process With ps Command

The ps command is a fundamental tool for displaying information about active processes. Let’s look at the following example:

Linux terminal ps command result.
Linux terminal ps command result. | Screenshot: Trung Thanh Le

When you type ps in the terminal, it displays a list of currently running processes, as you can see in the screenshot above. The columns TTY, TIME and CMD represent different attributes of these processes:

  • TTY stands for “Teletypewriter” and refers to the terminal or console associated with the process. In Unix-like operating systems, each terminal session is assigned a unique TTY number. The TTY column indicates which terminal the process is connected to. If a process is not associated with a terminal, this column will display a question mark (?).
  • The TIME column represents the cumulative CPU time used by the process since it started. It’s typically displayed in hours, minutes and seconds. This column provides insight into how much CPU time a process has consumed during its lifetime.
  • CMD stands for “command” and displays the command that was used to launch the process. This column shows the name of the executable or script associated with the process. If the command was truncated due to limited column width, an ellipsis (...) may be displayed at the end.

By default, ps provides a snapshot of processes running in the current shell session. However, with the appropriate options, such as aux, ef or www, ps can display detailed information about all processes running on the system, including their PIDs, CPU and memory usage and associated command. This information can be helpful in discerning which process to terminate. Here’s what each option does.

Show a List of All Processes With aux Command

aux is a commonly used option with the ps command.

  • a stands for all processes (including those of other users).
  • u displays detailed information about each process, including the user who owns the process, the CPU and memory usage and the command that launched the process.
  • x includes processes not attached to any terminal (i.e., background processes).

Together, aux displays a comprehensive list of all processes, providing detailed information in a user-friendly format.

Show a Hierarchical List of Processes With ef Command

The option ef provides a full-format listing of processes.:

  • e displays information about all processes, including those without a controlling terminal.
  • f displays a full listing that includes additional information such as the process hierarchy (parent-child relationship) and process group ID.

ef is commonly used to obtain a hierarchical view of processes, which can be useful for understanding the relationship between processes and their parents.

Make All Process Information Visible With www Command

www is specific to Berkeley Software Distribution (BSD)-based systems like FreeBSD and macOS, which isn’t commonly available in Linux distributions but still worth mentioning.

  • w displays wide output, which is useful for ensuring that all columns of information are fully visible, especially for commands with long arguments.
  • w is particularly helpful when viewing processes with lengthy command names or arguments.
  • w is often combined with other options like -aux or -ef to ensure that the output is properly formatted and all information is visible.

2. Find a PID With pgrep Command

Another useful command for locating processes is pgrep. Unlike ps, which requires parsing its output to find specific processes, pgrep simplifies the process by allowing you to search for processes by name and output their respective PIDs directly. Let’s say you want to search for “chrome,” you’d type the following into the terminal: pgrep chrome and would get the following results:

pgrep chrome results in Linux.
pgrep chrome results in Linux. | Screenshot: Trung Thanh Le

pgrep also provides options to search for processes based on criteria such as the user who owns the process, the parent PID or the session ID. You can refer to the pgrep man page (man pgrep) for more information and additional examples.

3. View Running Processes With top Command

Additionally, top provides a dynamic overview of running processes, CPU usage, memory usage and other system metrics to monitor system resource usage in real-time.

To launch the top command, simply open a terminal and type top, then press “Enter.” This will launch the top command in its default mode, displaying an interactive table with system resource information. There are plugins to make the top interface look more fancy. But the default style looks like this:

Top command result in Linux
Top command result in Linux. | Screenshot: Trung Thanh Le

Upon launching top, you’ll see a dynamic, updating interface with several sections. The header displays system-wide information, including the current time, how long the system has been running, the number of users logged in and system load averages.

Below the header, you’ll find a list of running processes. Each process is displayed on a separate line, with columns showing details such as process ID (PID), the user who owns the process (USER), process priority (PR), niche value (NI), virtual memory usage (VIRT), resident memory usage (RES), shared memory usage (SHR), process status (S), CPU usage (%CPU), memory usage (%MEM), total CPU time (TIME+) and the command that launched the process (COMMAND).

More on Software DevelopmentHow to Get the Last Element in an Array in JavaScript

 

How to Kill a Linux Process

Now that we know how to identify a process, we can now terminate it. For terminating a process, there are several commands to do so with their own advantages and disadvantages. Let’s have a look at them one by one.

1. Kill Command

The kill command is the quintessential tool for terminating processes in Linux. It sends signals to processes, requesting them to terminate gracefully. By default, kill sends the SIGTERM signal, allowing the process to perform any cleanup tasks before exiting.

Here’s how to use the kill command:

  1. Identify the PID of the process using ps, pgrep or top like I showed in the previous section.
  2. Execute the following command, replacing <PID> with the actual PID of the process: kill <PID>.
  3. After executing the kill command, you can verify whether the process has been terminated using ps, pgrep or similar command. If the process is not listed anymore, then you know the termination was successful.

If the process does not respond to SIGTERM or needs to be terminated immediately, you can use the SIGKILL signal by specifying -9:

kill -9 <PID>

While SIGKILL guarantees termination, it doesn’t allow the process to clean up resources, potentially leading to data loss or corruption.

The kill command allows you to send various other signals to processes. Each signal has a specific purpose, such as requesting a process to terminate gracefully (SIGTERM), immediately terminating a process (SIGKILL) or interrupting a process (SIGINT).

Here’s how you can use the kill command to send the SIGINT signal to a process:

kill -INT <PID>

In this command -INT specifies the signal to send to the process. -INT is the short form for SIGINT. <PID> is the Process ID of the target process.

For example, let’s say you have a process with PID 1234 that you want to interrupt (send SIGINT signal). You would execute the following command:

kill -INT 1234

This command sends the SIGINT signal to the process with PID 1234, which typically causes the process to interrupt its execution gracefully. Processes can handle the SIGINT signal by performing cleanup tasks or terminating gracefully, depending on how they are programmed.

Additionally, you can use the signal number instead of the signal name. For example, 2 is the signal number for SIGINT. So you can achieve the same result using:

kill -2 1234

Both forms (-INT or -2) are equivalent and will send the SIGINT signal to the specified process.

2. Killall Command

The killall command offers an alternative approach to terminating processes by allowing you to specify the process name instead of the PID. This can be useful when dealing with multiple instances of a process or when the PID is unknown.

You would use the killall command like the following:

  1. Identify the name of the process to be terminated.
  2. Execute the following command, replacing <process_name> with the name of the target process: killall <process_name>.
  3. After executing the killall command, you can verify whether the process has been terminated using ps, pgrep, etc.

This command sends the SIGTERM signal to all processes with the specified name, requesting their graceful termination.

3. Pkill Command

Beyond kill and killall, there are several other commands and techniques for terminating processes in Linux. Similar to pgrep, pkill allows you to search for processes by name and terminate them.

Here’s how to use the pkill command to terminate a process:

  1. Identify the name of the process to be killed.
  2. Execute the following command and replace <process_name> with the name of the process to be terminated: pkill <process_name>.
  3. After executing, you can verify whether the process has been terminated.

pkill offers more advanced pattern matching capabilities, making it useful for targeting specific processes.

4. Systemct1 Command

If your system uses systemd, you can manage processes using systemctl. For example, to stop a service, you would use:

systemctl stop <service_name>

5. Killproc Command

Some distributions provide killproc as a script for stopping processes associated with a particular daemon. You can use the following command to kill a process via it’s PID:

killproc -p <PID>

If you want to terminate a process by its name, you can also use this command:

killproc <process_name>

6. Top Command

Using the top command shows an interface that lists the currently running processes and their PIDs. At the bottom of the top interface, there’s a command line where you can enter interactive commands to manipulate the display or change settings. 

Press k to kill a process. You’ll be prompted to enter the PID of the process you want to kill.

More on Software DevelopmentWhat Is Caching?

 

What Happens When You Kill a Process in Linux?

While it is essential to know how to terminate processes, it is equally crucial to understand which processes can be safely terminated without causing system instability or data loss. Generally, user-owned processes or non-essential system processes — like user applications, background processes or services not critical to system operation— can be safely terminated. However, terminating critical system processes or those owned by the root user can have adverse effects on system functionality and stability. 

Terminating processes in Linux is a fundamental skill for system administrators and engineers  —  no matter what level they are. Remember to exercise caution when terminating processes, especially critical system processes, to avoid unintended consequences. With practice and experience, you can confidently manage and terminate processes to ensure the smooth operation of your Linux system.

Frequently Asked Questions

There are several scenarios in which terminating a process may be necessary:

  • Unresponsive or malfunctioning processes can lead to system slowdowns or freezes, impacting overall system performance.
  • Misbehaving or memory-intensive processes can consume excessive system resources, affecting the performance of other applications and services. By terminating the process you are reclaiming the system resources.
  • Terminating processes associated with malware or unauthorized activities is essential for maintaining system security and integrity.

The primary commands used to terminate processes in Linux are:

  • kill
  • killall 
  • pkill

Each command offers different methods for identifying and terminating processes, catering to various use cases and preferences. Depending on your distribution, you can also use systemctl or killproc to terminate a process. Understanding when and how to use these commands is essential for effective process management.

Explore Job Matches.