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. Follow this guide to learn how to terminate a process in Linux.

Written by Trung Thanh Le
Published on Apr. 18, 2024
How to Kill a Process in Linux
Image: Shutterstock / Built In
Brand Studio Logo

In Linux systems, adeptly managing processes is paramount. From optimizing system performance to troubleshooting issues, the ability to effectively control processes is a critical skill ,  no matter whether you’re a junior or senior Linux engineer respectively administrator. At times, however, you may encounter a misbehaving or unresponsive process that requires immediate termination. In such cases, having a thorough understanding of how to identify, assess and kill processes in Linux is invaluable.

5 Commands to Kill a Process in Linux

  1. Kill: This command sends the SIGTERM signal, allowing the process to perform any cleanup tasks before exiting.
  2. Killall command: This command allows you to specify the process name instead of the PID
  3. Pkill: This allows you to search for processes by name and terminate them.
  4. Systemcl: If your system uses systemd, you can manage processes using systemctl.
  5. Killproc: Some distributions provide killproc as a script for stopping processes associated with a particular daemon.

This comprehensive guide aims to equip you with the knowledge and tools necessary to confidently terminate processes in Linux. We’ll discuss various methods and their options of locating processes, explore the arsenal of commands available for termination and gain the understanding which processes can be safely terminated. By the end, you should have a solid understanding of how and when to kill a process in Linux.

 

Locating the Process

Efficiently terminating a process begins with identifying its Process ID (PID). Understanding how to locate processes is crucial for effectively managing and terminating them.

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.

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

 

1. 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.

 

2. 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.

 

3. www Command

wwwis 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.

 

4. 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.

 

5. 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).

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. In order to navigate and interact with interface, here is what you can do:

  • Use the arrow keys (up and down) to navigate through the process list.
  • Press q to quit top.
  • Press k to kill a process. You’ll be prompted to enter the PID of the process you want to kill.
  • Press h to display the top command help screen, which lists available interactive commands.
  • Press 1 to toggle between showing all CPUs or a summary of CPU usage.
  • Press u to filter the process list by user. You’ll be prompted to enter the username whose processes you want to display.

As you can see, top provides various command-line options for customization, allowing you to specify the update interval, sort order, and which columns to display. You can also create a top configuration file (~/.toprc) to save your preferred settings for future use.

 

Commands to Kill a Process in Linux

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.

 

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.

 

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.

 

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.

 

Other Kill Commands to Know

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

systemctl stop <service_name>

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>
A tutorial on how to kill process in Linux. | Video: HOWTECH

More on Software DevelopmentWhat Is Caching?

 

What Processes Can You Kill 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. Terminating critical system processes or those owned by the root user can have adverse effects on system functionality and stability. Therefore, it’s important to exercise caution when terminating processes is paramount.

Generally, user-owned processes or non-essential system processes can be safely terminated. Examples include user applications, background processes and services not critical to system operation. However, system-critical processes or those essential for system operation should be handled with care to avoid disrupting system functionality.

Adeptly terminating processes in Linux is a fundamental skill for system administrators and engineers — no matter what level they are. By proficiently locating processes, leveraging an array of termination commands, and discerning which processes can be safely terminated, one can maintain system stability and performance effectively. Armed with the knowledge imparted in this guide, you are well-equipped to navigate the intricacies of process termination in your Linux environment. 

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.

Hiring Now
2K
Gaming • Information Technology • Mobile • Software • eSports
SHARE