System Administration

How to kill a process on Linux

There will likely be times when you encounter issues with unresponsive applications and processes. Sometimes closing and even restarting them does not work. In such cases, the only option that comes in mind is to restart the system which is time taking process and sometimes not acceptable in case of servers running a number of critical services.There are some other useful ways to get around this issue by terminating the process without needing to restart the system. This article will explain such ways which can be used to kill a process in a Linux OS.

Note: We have explained the procedure mentioned in this article on Ubuntu 20.04 LTS. More or less the same commands and procedures can be followed in previous versions of Ubuntu,

Using the System Monitor

The Gnome system monitor is a built-in GUI utility that can be used to kill a process in Linux OS. It allows to stop a process and then resume it with the Continue option. The end option allows terminating a process safely while the kill option forcefully terminates the program.

If System Monitor is not installed in your system, you can install it as follows:

$ sudo apt-get install gnome-system-monitor

To launch System Monitor, hit the super key and search it using the search bar at the top. When the search result appears as follows, hit Enter to open it.

In the System Monitor window, you will see a list of processes running in your system. In order to kill a process, right-click it, and select Kill option. It will kill the selected process instantly.

Kill process using the keyboard shortcuts in Terminal

Linux command line includes a number of useful keyboard shortcuts. Among them, following shortcuts can be used to kill a running process.

Ctrl+C: It sends SIGINT that terminates the running process

Ctrl+Z: It sends SIGSTP that suspends an application and send it to the background. However, it does not terminate the process. To view the stopped process, you can use the jobs command. Use fg command to bring the process to the foreground.

Ctrl+\: It sends SIGQUIT that terminates the process. It also creates a core dump file which can be used to debug the process.

Kill process using the xkill utility

Xkill allows killing a running program using the mouse cursor. It is GUI based utility that is pre-installed in most of the systems. If not already installed, you can install it as follows:

$ sudo apt install xorg-xkill

To close any program, simply type this in your command line Terminal:

$ xkill

Running the above command will turn your mouse cursor in to x shape. Now place the cursor on the program that you want to close and left-click on it.

Set shortcut for Xkill

You can set a shortcut for xkill that will allow you to immediately kill an application without the need of opening the Terminal and running the command.

To create a shortcut for xkill, open the Settings utility using the right-click menu from the desktop. Then open the Keyboard Shortcuts tab and click the + icon at the very bottom.

Then in the following dialog, name the shortcut and type xkill in the Command field and click Set Shortcut button.

Then set a custom shortcut of your choice and click the Add button.

Now whenever you need to kill an application, simply press the shortcut keys and you will be able to kill any open application in your system.

Kill process using the Kill commands

There are also some command-line ways used to kill the processes in Linux which includes kill, pkill, and killall.

To find which processes are currently running in your system, you can use the ps command with –A flag:

$ ps –A

It will list all the currently running processes in your system.

Kill

Kill command can be used to kill a running process in Linux. The kill command is provided with a PID of a process to be killed.

To find the process ID of a running process, you can use ps –A command. Alternatively, you can pipe the output of ps with grep command to find the process ID of a specific process:

$ ps –A | grep <processname>

For example:

To find the process ID of arunning Firefox program, you can use:

$ ps –A | grep firefox

Once you have found the PID of a specific process, you can kill it as follows:

$ kill PID

The kill command sends a SIGTERM signal to the specified PID which asks the process to terminate after performing the necessary cleanup operation.

In some scenarios, running the kill command does not terminate the process. If this is the case, you will need to type “kill -9” followed by the PID:

$ kill -9 PID

Using the -9 option with kill command sends a SIGKILL signal which asks the process to terminate immediately without any cleanup operation.

Pkill

Similar to kill command, pkill also sends a SIGTERM signal which allows terminating an unresponsive process. However, the good thing about pkill is that you do not have to provide the PID of a process in order to kill it. Instead, you can just provide the matching keyword related to the process.

$ pkill <keyword>

For example, to kill Firefox program, you can just type:

$ pkill firef

It will kill all the processes whose names match with the mnentioned <keyword>.

With pkill, you also have an option to kill the process running by a specific user:

$ pkill –u <username> < keyword>

Be careful when using this option as If you do not specify the <keyword>, all the processes with the specified username will be killed.

Killall

Killall command is similar to pkill except it takes the full process name as an argument instead of any matching keyword.

In order to use killall to terminate all the processes and their child processes with a specific name, use the following syntax:

$ killall <prcoessname>

For example:

$ killall firefox

That is all there is to it! In this article, you have learned various ways to kill a process in Linux. Use these commands with care as killing a process causes it to end immediately resulting in a data loss. Also killing the wrong process might end up disturbing the system.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.