How to kill process on Linux

Every program that is running at the end of the server is able to generate one or more processes. A single server may have several users that execute many commands that will make processes. These processes may run the foreground as well as the background. If a program is running in the foreground it can occupy the terminal where the program was initially started, and other applications cannot even be started until other processes are still running the foreground. Whereas, other processes can also run in the background, in this case, the terminal in which we began started our program can add further new commands while the program is still running. Daemons are also a type of process that keeps on running continuously. They listen for requests on particular ports. They are usually initialized at system startup and just wait in the background and keep themselves in an idle state until their service is needed.

At times while working, our system becomes unresponsive, a process may also take a lot of system space and may even halt. So, we need to find and kill such process in Linux to proceed further. There are many ways to kill a process in Linux which will be discussed in this tutorial. A process can be killed forcefully by these commands.

I have used Ubuntu while implementing this article. However, commands can work on any Linux distribution.

Locating a Linux process

Step 1: First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Find Linux terminal

Step 2: Now click on the Terminal and wait for the terminal to open.

Start Linux terminal application

 

Step 3: Once the terminal is opened, you will have a screen like this:

Shell window is open

The two keys important here in upcoming steps are as follows:

  1. PID – It is the Process ID. Every single process has it’s a specific 5-digit number assigned to the process itself. The numbers can be repeated when they reach a maximum limit but are always unique for a process in a system.

Step 4: top – command is used as a basic command to view all of the currently running processes that are utilizing the resources of a computer device. So, we execute top- command in the terminal and the results are

Use Linux top command to locate a process

top is itself an application. It can be used to display the list of all processes and it has a new layout when it is being executed. Here is the example of it,

  • h – Is used to display a help window. The help window has all of the commands, they provide information that is quite useful.
  • space – On pressing the space, we will get to update the process table immediately. We will not be made to wait for any time interval.
  • f – to add files to display in the layout or remove to remove particular certain fields in order to stop them from being displayed.
  • q – it is used to quit the top application and also used for additionally opened window
  • l – it is used to switch the display of load average and uptime information.
  • m – it is used to switch the display of the information of memory.
  • P (Shift + p) – it is used to sort the processes by the usage of CPU.
  • s – it is used to change the delay between refreshes.

Step 5: There is another command also to see the list of all currently running processes, we use “ps –A | less” command.

Linux ps command

The list of all of the currently running processes will appear. This process shows all running processes that are having their associated PIDs “process IDs”. You can further look through this list to find out the name of the process that also matches the program you actually desire to kill. The programs that run most recently are displayed at the end of the list which you want to kill from the list. For our example, we have used Firefox as a process of our interest to be killed.

Processes and their PIDs

Once you are done press “q” to exit this window.

Killing the process by ID

Once we get the PID we can use the kill command to kill a process from the information we retrieved from ps –A command.

Step 1: We use “sudo kill PID” command for this purpose. Where PID is the process ID we want to kill.

Linux kill command

 

If we want to kill a specific process, we see its corresponding PID and then use it to kill that running process. E.g. we want to kill “firefox” process so, we find its PID at first.

Process list

So, here the PID of firefox is 9999. We use “sudo kill PID” to kill the process of Firefox.

Kill Firefox process

How to check if the process has been killed or not?

Then we check all running processes once we kill the one of our interest, using “ps –A | less” command”. The outcome shows that we have no firefox process (PID: 9999) in the list.

Check if the process has been killed

Killing the process by name

To Kill a program using the name of its process only works for all of the executable files (i.e., all of the runnable programs) that are meant to be open at the time of execution. All of those programs that are running in the background are not capable of being killed using the process name.

We will now kill the process of our interest using the name of that process. If we want to kill a specific process, we see its corresponding PID and then use it to kill that running process. E.g. we want to kill “firefox” process so, we find its PID at first. Kill Linux process by name

So, here the process name is firefox and we use “pkill firefox” command for killing this process.

Using pkill command

How to check if the process has been killed or not?

Then we check all running processes once we kill the one of our interest, using “ps –A | less” command”. The outcome shows that we have no firefox process in the list.

Check if pkill command has killed the process

Conclusion

Thanks for reading this article. We have seen how we can locate and then kill a process by using its process ID or name.

How to locate and kill a process with Linux terminal