How to Kill a Process in Linux

We have all had those days when our computer runs exceptionally slow and stutters in performing even the basic tasks. In Windows, you only have the Windows Task Manager to inspect and kill processes that are not so important but consume enormous amounts of memory. However, in Linux, you have an entire arsenal of commands and GUI tools to manage these tasks. In this article, we will show some easy command line (CLI) methods as well as GUI methods for how to kill a process in Linux.

But before we dive into the world of process management in Linux, let us understand what exactly a process is and what is a process ID in Linux systems.

What are Processes in Linux

In Linux, each currently running instance of a program is known as a “process,” while each executable file is known as a program. When any program is executed, a process is created and each process is assigned a unique 5-digit identification number known as “process ID”. When a process has finished executing or is terminated forcefully, its process ID gets assigned to the next-in-the-line process.

Kill Process via Command Line in Linux

Though using the terminal may sometimes seem intimidating as compared to using GUI tools for basic tasks, managing various processes becomes much easier once you get a grasp of the commands and their various options.

Termination Signals

When you try to kill a process either from the GUI or the CLI in Linux, the kernel sends a termination signal to the process. The process acts accordingly, depending upon the signal received. Each of these signals is assigned a specific number for the program to understand quickly. There are numerous types of termination signals, but we have explained only the essential ones here:

SignalNumeric ValueDescription
SIGHUP1It stands for ‘Signal Hangup’
It is sent when the terminal is closed.
SIGINT2It stands for ‘Signal Interrupt’
It is sent when the user terminates the process.
SIGKILL9It stands for ‘Signal Kill’
It is sent when you need to quit a process immediately
SIGTERM15It stands for ‘Signal Termination’
It is sent when you need to terminate a process and release the resources consumed
SIGSTOP19 – for ARM, x86
17 – for ALPHA
23 – for MIPS
24 – for PA-RISC
It stands for ‘ Signal Stop’
It is sent when you need to pause a process and resume it later
The most commonly used signals are the SIGKILL (9) and the SIGTERM (15).

Identify the Process IDs

Before you terminate a process, you need to know some details of the process such as the process ID, running time, etc. To know the details of a process, use the ps command:

ps

With the ps command, you need to search for the process by scrolling and noting its name, which could be cumbersome. Instead, you can even use the grep command with the ps command in a pipeline, as shown below:

ps | grep <process_name>

To make things simpler, there is a separate command that shows only the process ID of any running process you need to know. The syntax to use the pidof command is:

pidof <process_name>

Identifying the process id

Terminate Process using the kill Command

Once you have noted the process ID of the process you want to terminate, the most common command used to terminate programs on your Linux system is the kill command. The syntax to use the kill command is:

kill <signal> <process_id>

The <signal> parameter is optional and the kill command sends the SIGTERM (15) signal by default. You can send any other signal by its numerical value or the actual signal name from the table above.

Terminate Process using the pkill Command

If you feel that searching for process id is inconvenient, you can use the pkill command. It looks for the processes matching a pattern and then kills it. The syntax to use the pkill command is:

pkill <options> <pattern>

Some of the common options to pair with the pkill command are:

OptionDescription
-nSelects only the latest processes matching the process id
-uSelects the processes owned by a particular user
-xSelects processes matching the pattern exactly

This is particularly important when multiple users are working on different instances of the same program in the same system and one of the instances starts to have some unexpected behavior. For example, in the below screenshot, we are killing the “gedit” instance owned by the user ‘intel’ using the pkill command in Linux:

pkill -u intel gedit

Terminate Process using killall Command

The killall command works similarly to the kill command but it kills all the processes matching the process name irrespective of the user. By default, it sends the SIGTERM signal if not specified otherwise. An interesting thing to note is that the killall command can never kill itself but can terminate other instances of the killall command. The syntax to use the killall command is:

killall <options> <process_name>

Some of the options to pair with the killall command are:

OptionDescription
-rInterprets the process_name as a regex pattern and then kills the processes matching the pattern
-ukills the specified process owned by a specified owner.
-oKills the specified processes older (started before) than a specific time.
-yKills the specified processes younger (started after) the specific time.

The killall command can be useful to terminate a series of the same process or even all the processes owned by a specific owner. Here in our example, we are killing all the processes of “sleeping for 500 seconds” using the killall command in Linux:

killall -v sleep

Terminate Linux Process using top/ htop Command

This method of killing processes is useful when you have no clue which processes are consuming max resources. In both commands, you can navigate through all the running processes or even zombie processes and can end them quickly. To use the top command to monitor your resources, use the syntax in the Terminal:

top

Understanding the Output:

The main output of the top command is divided into columns which are:

  1. PID – shows the process ID of the running process
  2. User – shows the owner of the process
  3. PR – shows the priority value of the process which is assigned by the operating system
  4. NI – shows the nice values which are like assigning user-spaced values to manually control the priority of a task.
  5. VIRT – shows the amount of virtual memory used by the process.
  6. RES – shows the amount of physical memory used by the process
  7. SHR – shows the amount of memory shared by other processes
  8. S – shows the current state of the process which can be:
    • D – uninterruptible sleep
    • R – running
    • S – sleeping
    • T – stopped
    • Z – zombie
  9. %CPU – Shows the amount of CPU utilized by the process in percentage
  10. %MEM – Shows the amount of RAM utilized by the process in percentage
  11. TIME+ – Shows the total running time of the process
  12. Command – Shows which command was invoked for the process.

If you don’t know the process ID of the task you want to kill, either navigate through the list using the arrow keys or search for the process name in the process table in Linux.

To search the process name, press ‘L’ on the keyboard and type in the process name you want to search. Once you find the nefarious process, press ‘k’ on the keyboard to kill the process. Now, enter the process ID or leave it at the currently highlighted process and press ‘ENTER’. Next, input the termination signal and then press ‘ENTER’ to kill the program. To return back to the terminal, press ‘q’ on the keyboard.

Although the top command shows details such as process ID, memory consumption, and more for all the running processes, it is not well suited for beginners as it does not show any key mappings or how to use it. On the other hand, the htop command has a more user-friendly interface, even for a command line tool. Plus, it shows all the details in a separate view, hence, it does not clutter the terminal window. It does not come preinstalled in most distros, and you need to use the following command to install htop in Linux:

sudo apt install -y htop

To use htop to manage processes in Linux, use the command below:

htop

To kill a program, navigate to the process name you want to terminate, press ‘F9’ and then press Enter. If you need to search and kill any program, press ‘F3’ on the keyboard, type in the name and hit Enter. The process name will be highlighted, press F9 and then press Enter on the keyboard to terminate the process.

Kill a Process via System Monitor in Linux

If you feel that the command line method is difficult for you, you can use the built-in system monitor tool that’s available on every Linux distribution. To learn how it works, open the system monitor tool from the Applications menu and follow the steps below.

1. Once the system monitor tool opens, you will see three tabs on top named — Processes, Resources, and File System. To manage your processes, head over to the “Processes” tab. Here, you will see all of your currently running processes. Press ‘CTRL+F’ to search for a process name. Click on the process name you want to terminate and click on “End Process”.

2. Then, you will get a confirmation prompt whether you want to end the process or not. Go ahead and click on the big red “End Process” button to kill the process in Linux.

Killing memory hogging processes is quite a crucial task that every user should learn. In this article, we have shown both the command line method, including commands like killall and pkill, as well as the GUI methods to kill processes in Linux. We have even detailed how to use popular tools like top and htop to manage processes on your Linux PC. If you face any issues while using these tools to kill a process, do let us know in the comments below.

How do I stop all processes in Linux?

If you need to stop all processes (except the login shell, init, and kernel-specific processes) for a specific user in Linux, use either the pkill command or the killall command as per the syntax:
pkill -u <username>
killall -u <username>
If you need to kill every process for every user including the init system, press the ‘ALT + Prt Sc + o’ keys on the keyboard.

Is it OK to end a process?

When you close any non-essential background process or a user process that is consuming a lot of system memory, you free up the resources which now can be utilized by other processes. But, before shutting down any process, make sure that you’re not killing an essential operating system process.

What are the background processes in Linux?

In Linux, background processes are processes that can run without the shell instance or any user intervention. They can be viewed using any of the commands – top, htop, ps, etc.

What does CTRL + Z do in Linux?

When you use CTRL + Z in Linux, it sends the SIGTSTP signal which suspends the process and sends it in the background. When a process is in a suspended state in the background, you cannot kill the process until it is brought back into the foreground.

What is a zombie process?

A process that has been killed by the user but is still occupying memory is known as a zombie process.

Comments 1
Leave a Reply

Loading comments...