How To Kill a Process in Linux

How To Kill a Process in Linux
(Image credit: Tom's Hardware)

In this how-to we’ll look at various ways of using the terminal emulator to identify processes and how to kill them. A process can be an application or script running on your Linux machine. Sometimes a process can crash or become a memory hog and this is when we need to step in and “kill” the process. As ever, there are a multitude of tools that we can use to do this. We will use a variety of approaches and tools to identify and kill the processes.

While you become accustomed to these commands, it’s important to take care that you don’t kill a process which results in you losing work. Wherever possible use an application that isn’t currently in use on your machine as an example of a process to kill.

All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.

How to Kill a Known Application in Linux 

(Image credit: Tom's Hardware)

To begin, let's open a software application and then, in the terminal, use the killall command to kill the application. We have used Inkscape, the free and open source vector graphics package as an example, but it doesn’t really matter what you use, just so long as you aren’t depending on it for anything at the current time.

Using the killall command kills all processes linked to the named application. Later we will look at using the kill command to kill a specific process.

1.  Launch Inkscape, or another test application, from the system gui.

2. In the terminal, use the killall command to kill all Inkscape processes. You should see that Inkscape closes immediately when this command is issued.

killall inkscape

Identifying Different Processes in Linux

(Image credit: Tom's Hardware)

Often, software applications have numerous processes running and it can be useful to try and work out which one is likely causing the problem. For example, it could be one browser window rather than all of Chromium or Firefox.Then you can close the problematic process while leaving the rest of the app running. We can use the top tool in the terminal to look at, interact and then kill the rogue processes.

1. Launch the top tool in the terminal. When it’s launched, you will see the terminal populate with information regarding all the processes and their status. The second line of the top screen shows the total number of tasks, which are currently running and which are sleeping, stopped or zombie status. Below this you will see a list of processes and this information will refresh at a default of once every three seconds.

top

2. Press i to constrain the current processes list to those currently active. This can be very useful if you are only interested in current running processes and makes the list easier to read.

3. Press z to add color to the top interface to make it easier to read. Because this is a text based interface, adding color can help distinguish between labels and data.

4. Launch Inkscape or another non essential application.

5. Find the listing for Inkscape and note the PID. The PID is the number assigned to identify the process and they are listed in the first column of the top output. In our example the PID for inkscape was 4582 but it will be different each time Inkscape is running.

6. Press k and then type the PID number to kill the process.

7. Press ENTER and you should see a message “Send pid 4582 signal [15/sigterm]” which is confirming you want to kill this process. Press “enter” once more to confirm and kill the process.

8. Press q to close top and return to the terminal emulator. Often, you will want to look at top for a quick glimpse at the processes and then return to the terminal.

Killing an Individual Process Outside of top

(Image credit: Tom's Hardware)

Occasionally you might want to kill an individual process outside of top. We can use the PID number found via top in combination with the kill command to do this. Note that the killall command we used earlier won’t work with an individual PID.

1. Launch Inkscape or another application. The choice of application doesn’t really matter, just make sure you aren’t working in the application.

2. Launch top in the terminal. Press i and z to constrain the results and add color to the top theme.

top

3. Identify and note the PID for the application to kill. In our example the PID for Inkscape was 4582.

4. Exit top by pressing q.

5. Use the kill command and the PID number to kill the Inkscape process from the terminal emulator. Replace the example PID value below with your own identified PID.

kill 4582

Using top provides an excellent set of detective tools to identify misbehaving processes and can often allow you to clinically close an unresponsive process whilst possibly saving other data in an application.

Killing Processes Using the ps Process Report

There is an alternative to top. We can use the ps command, designed to produce a report of all the running processes.

1. Launch Inkscape or another application. The choice of application doesn’t really matter, just make sure you aren’t working in the application.

2. Run the ps command with two arguments. The -e argument shows all the processes and the -f argument sets the format to the system standard syntax.

ps -ef

(Image credit: Tom's Hardware)

3. Use grep to filter the report. searching just for “inkscape”. The typical ps -ef output is extremely dense, packed with dozens of running processes. Using a filter we can search for the application name. The PID is the second column, just after the username. You may see multiple PIDs for the application.The one that we are interested in just shows the application name.

ps -ef | grep inkscape

(Image credit: Tom's Hardware)

4. Use the kill command along with the PID to kill the process. In our case Inkscape had a PID of 19166.

kill 19166

Process management is a key Linux skill to learn and it’s worth taking the time to practice these approaches before you need to deploy them in a real situation.

Jo Hinchliffe

Jo Hinchliffe is a UK-based freelance writer for Tom's Hardware US. His writing is focused on tutorials for the Linux command line.  

  • Sergei Tachenov
    I find pkill to be most useful. But this is probably because I often need to kill Java processes, and they all share the same executable, so killall java is obviously not an option. With pkill, I can kill a process not by its name, but by its argument list, e. g. pkill -f some-app.jar.
    Reply
  • hotaru.hino
    I like to use ps aux | grep -i if I need to look for something specific to kill
    Reply
  • Sergei Tachenov
    If often use that one too, but sometimes pgrep works better. Especially in scripts, where it’s important that the process search command doesn’t match itself.
    Reply
  • Kill tout
    I use xkill because its more fast when my pf get fire... just dropdown terminal emulator and xkill with touchscreen
    Reply
  • strayling
    kill -9

    It's the only way to be sure.
    Reply
  • jcipale
    Anyone who has worked with unix/linux for more then a month learns this. This article is just another example of someone getting paid by the word.
    Reply