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:
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 thepscommand:
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 thegrepcommand with thepscommand 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 thepidofcommand is:
pidof <process_name>
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 <process_id>
Theparameter 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 Processusing 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
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
Thekillallcommand works similarly to thekillcommand but it kills all the processes matching the process name irrespective of the user. By default, it sends theSIGTERM signalif not specified otherwise. An interesting thing to note is that thekillallcommand can never kill itself but can terminate other instances of thekillallcommand. The syntax to use thekillallcommand is:
killall <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:
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 keyboardand 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 thetopcommand 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,thehtopcommand 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 installhtopin 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. Theprocess 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.
-
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”.
-
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.
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 killall -u If you need to kill every process for every user including the init system, press the ‘ALT + Prt Sc + o’ keys on the keyboard.
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.
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.
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.
A process that has been killed by the user but is still occupying memory is known as a zombie process.
Prayash Kumar
Add new comment
Name
Email ID
Δ