Ubuntu

How Do You Kill a Process In Ubuntu Terminal?

One of the best features of any Linux distribution is the advanced Command Terminal. Using the Command Terminal, you can execute long and complicated tasks by just writing a few lines of commands that would otherwise require a long and complicated procedure on the GUI.

Although familiarizing yourself with the Terminal may seem daunting at first, it will become your most valuable asset in your journey to master Ubuntu with time and practice.

An example of a task made easy with the help of the Command Terminal is killing a process in Ubuntu. Although there is a GUI method for killing processes, it is long and involves many steps.

The CLI method is easier as it allows you to kill processes by typing a few commands in the Terminal.

If you find the Command Terminal daunting or wish to learn the CLI method of killing processes in the Terminal, then this guide is meant for you. By the end of this guide, you’ll have a firm grasp on the commands you can use to kill processes in the Ubuntu Terminal.

Let’s take a look at the commands.

Using the $kill Command

To kill any process in Ubuntu, you can use the $kill command. The $kill command is convenient in quickly ending a process. The syntax for the command is as follow:

$kill -signal <Process ID>

The syntax consists of two parts, i.e., signal and Process ID. Details of these commands are given below.

The Signal Option

Signal refers to a number that designates a signal name. There are a total of 64 available signals. You can type the following in the Command Terminal to view them all.

$ kill -l

The output should be similar to the one shown in the image below:

Graphical user interface, text Description automatically generated

Two are the most commonly used from the list of 64 signals that come with the $kill command.

The first one is option 9 that is SIGKILL. This signal is used to terminate the process without saving any sort of data and is seen as a last resort when it comes to killing a process.

The second one is option 15, referred to as SIGTERM. As the name suggests, it will terminate a given process. This is the default signal to kill a process.

You can implement signal in three ways:

  • Via signal number: $ kill -15 <Process ID>
  • Via signal name: $ kill sigterm <Process ID>
  • Via signal name without the sig: $ kill term <Process ID>

The Process ID

The Process ID is a number that is unique to each process in Linux. The Process ID ensures that you’re killing the correct process.

Although it may look like you’d need to memorize PIDs for different processes, that is not the case. You can type the following command in the Terminal to find the Process ID of any process.

$ pidof <name of process>

$ pgrep <name of process>

In our case, we’re going to find the PID for Firefox.

$ pidof firefox

Text Description automatically generated

$ pgrep firefox

Now that you’ve learned how signal and PID work, it’s time to learn how you can kill a process with their help.

Killing A Process Using The $kill Command:

With the prerequisites out of the way, let’s try to use the $kill command to terminate a process. Although we’re going to be using Ubuntu 20.04 LTS for this guide, the method shouldn’t be any different for other versions of Ubuntu. Let’s take a look at the steps to kill a process.

Start by opening the Command Terminal on your system; the shortcut for this command is Ctrl + Alt + T. Once the Terminal opens, get the PID of your process by typing any of the two commands for PID.

$ pid firefox

Text Description automatically generated

Or

$ pgrep firefox

This should present you with the PID of the required process.

Once you have the PID, proceed to kill the process by typing one of the following commands

$ kill -15 <Process ID>

$ kill sigterm <Process ID>

$ kill term <Process ID>

In our case,

$ kill -15 2484

Text Description automatically generated

This will terminate Mozilla Firefox.

If sigterm doesn’t work for you, use the following command to eliminate the process:

$ kill -9 <PID>

In our case,

$ kill -9 2484

This command will kill the process “Mozilla Firefox” without keeping any data.

Additional Uses of the $kill Command.

The $kill command is very flexible when it comes to its syntax. It can be used in various ways to complete the task as efficiently as possible.

An example of the flexibility provided by the $kill command is that it allows you to kill multiple processes in a single command. The syntax for the command is as follow:

$ kill -sigterm <PID1> <PID2> <PID3> and so on

Another feature that adds to the flexibility of this command is that it allows you to directly find the PID of a process in a single statement. This gets rid of the process of remembering the PIDs before killing a process.

The syntax for the command is as follow:

$ kill -sigterm pidof <process name>

In our case,

$ kill -sigterm pidof firefox

This will scan for the PID of the process and terminate it.

Although killing a process isn’t something you’d do daily, it’s necessary to learn it to get rid of unwanted or malfunctioning processes. Thus, knowing how to kill a process can help you save your system’s resources by eliminating all unwanted ones.

It should be noted that although all processes can be terminated, it is advised that you know what process you’re going to terminate. This is because terminating the wrong process can cause the linked processes to malfunction, creating more problems as a result.

Conclusion

We hope this guide helped you learn how you can kill a process with the help of the Command Terminal. We covered the basics of the $kill command, its syntax, and options. We also saw how it could be used to kill multiple processes simultaneously. With this, we wish you all the best in your journey to learn more about Ubuntu.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.