If a process becomes unresponsive and consumes too many system resources, then it’s time to kill it.

In Windows, every process can be shut down using a task manager. On Linux, most processes have their own way to shutdown. Few can quit using the q keyboard key, and some use the exit word.

Unfortunately, some processes become too obstinate to shut down using any recommended method and do not allow themselves to be shut down gently.

At that time, you can kill the process forcefully using the top, kill, pkill, killall, and xkill commands in Linux.

How Process is Killed?

Before you learn to kill any process, you need to understand how processes are killed behind the scenes to understand the most suitable methods and ways to approach killing any process.

Fun fact: Init is the master process that starts with system boot and is assigned PID 1. In a simple word, you cannot kill the init process unless init decides and allows itself to be killed (Ex: Shutdown).

Take a look below to understand what types of processes are allowed to kill.

  • The user can easily kill their own process.
  • But cannot kill other users’ processes.
  • System-level processes like Display Manager cannot be killed by normal users.
  • Root user is king, able to terminate anything in front of it (do not overdo, might crash your system).

A root user can kill any process by being root, or by either adding sudo before the command or entering the root shell using su.

In Linux, a signal is sent over the process while killing. Many signals can be used to guide the killing process. To get the complete list, use the below command.

$ kill -l

Below is the behavior of this command.

To get the list of all killing signals
To get the list of all killing signals

Regular users do not have to worry about all those signals. You just need to be aware of the 1, 9, and 15 signals.

Signal NameSignal ValueBehavior
SIGHUP1Hangup
SIGKILL9Kill Signal
SIGTERM15Terminate
  • SIGHUP is a less secure way to kill any process; align it between SIGKILL and SIGTERM.
  • SIGKILL is a forceful way to kill any process by generating a fatal error. It will work most of the time when the process is unresponsive, but leave the process unsaved.
  • SIGTERM is the default and safest way to gently kill any process.

Fun fact: Most of the time, SIGKILL signal is most effective way to kill any obstinate unresponsive process.

Once you understand all of the signals used to send while killing any process. The next thing requires is to find out the PID of any process while killing using the kill command.

Kill Process using Top Command

In Linux, the most effective and easiest way to kill any process is using the top command.

The top command-list all of the running processes in your system with their PID, memory, and CPU consumption.

$ top

Below is the behavior of this command.

List all of the running processes in your system
List all of the running processes in your system

To kill any process within the top interface, press the k keyboard button and enter the PID of the process as shown below.

Killing a process using the top interface
Killing a process using the top interface

Above you can see that I have killed Firefox with the command name “GeckoMain” with PID 6411 using the top interface.

List All of the Running Linux Processes

Before you kill any process, you need to find the PID of that particular process using mentioned method.

The pidof, pgrep, and ps commands are the most effective and easiest way to find the PID of any particular process besides all other commands like top in Linux.

pidof command

To know all of the running processes along with their PID without any fluctuations in output like a top command, run the following pidof command along with the process name.

$ pidof nano

Below is the behavior of this command.

Finding the process ID of the process
Finding the PID of the process

Above, you can find the PID for the nano editor is 7153, which can be used to kill it.

pgrep command

The pgrep command is a complex way to find any PID. The basic working requires knowing the regular expression.

You can use pgrep with process name without any flags to return the PID as shown below.

$ pgrep nano

Below is the behavior of this command.

List specific processes using the pgrep command
List specific processes using the pgrep command

ps command

The ps command is similar to the top command, except without any interaction and fluctuations. Flags can be added to extract the particular information along with the ps command.

$ ps <options>

Below are the most common flags used, along with the ps command.

  • a List all of the processes of the entire system instead of specific users.
  • u Provide detailed information about each process.
  • x List the daemon process handled using the systemctl command.
$ ps aux

Below is the behavior of this command.

List all of the running processes using the ps command
List all of the running processes using the ps command

You can pipe grep command along with the ps to output specific processes with the PID.

$ ps aux | grep nano

Below is the behavior of this command.

List specific processes using the ps and grep command
List specific processes using the ps and grep commands

Kill a Process in Linux using the Kill, Killall, Pkill, and Xkill Commands

In Linux, there are four different ways to kill any process, along with the top command, depending upon your situation.

If you found the PID of the process using any of the methods mentioned above, then you can kill the process using the kill command.

If you don’t know the PID, then you do not have to worry. Use the killall or pkill command to kill any process with the process name.

Note: All of the processes share the same name, meaning if you run the same process multiple times, then killall and pkill will kill all of the process instances and child processes.

Don’t know the PID or process name? Then xkill is the best option for you to kill any process with just one click.

Kill a Process using the Kill Command

The kill command can kill the process if you know the PID. By default, it sends a SIGHUP or SIGTERM signal to the process to shutdown.

$ kill 7394 #Replace the PID with your process.

Below is the behavior of this command.

Killing a process using SIGHUP or SIGTERM signal
Killing a process using SIGHUP or SIGTERM signal

If the above method didn’t work out, you could forcefully kill the process using a different signal like SIGKILL or its signal value 9, as shown below.

#Don't forget to replace PID
$ kill -SIGKILL 7406
#or
$ kill -9 7406

Below is the behavior of this command.

Killing a process using a different signal
Killing a process using a different signal

Either use the signal name or signal value; both work in the same way.

Kill a Process using the Killall Command

If you do not know the PID of the process, then you can use the killall command. The killall command sends a SIGHUP or SIGTERM signal to the process and can kill all of the processes with the same name.

For example, if you were running multiple nano processes in two different terminals. Then the killall command will kill both of the nano processes at once, as shown below.

$ killall nano

Below is the behavior of this command.

Killing all processes using SIGHUP or SIGTERM signal
Killing all processes using the SIGHUP or SIGTERM signal

If the above method didn’t work out, you could forcefully kill the process using a different signal like SIGKILL or its signal value 9, as shown below.

#Don't forget to replace the process name
$ killall -SIGKILL nano
#or
$ killall -9 nano

Below is the behavior of this command.

Killing all processes using a different signal
Killing all processes using a different signal

Either use the signal name or signal value; both work in the same way.

Kill a Process using the Pkill Command

The pkill command is similar to the pgrep command, it will send a SIGHUP or SIGTERM signal to the process and kill all processes with the same name.

For example, if you were running multiple processes of gedit. Then the pkill command will kill all of the gedit processes at once, as shown below.

$ pkill gedit

Below is the behavior of this command.

Killing all processes using SIGHUP or SIGTERM signal
Killing all processes using SIGHUP or SIGTERM signal

If the above method didn’t work out, you could forcefully kill the process using a different signal like SIGKILL or its signal value 9, as shown below.

#Don't forget to replace the process name
$ pkill -SIGKILL gedit
#or
$ pkill -9 gedit

Below is the behavior of this command.

Killing all processes using SIGHUP or SIGTERM signal
Killing all processes using a different signal

Either use the signal name or signal value; both work in the same way.

How do I kill a process that freezes everything so I can’t open a terminal?

In that case, a reboot is the preferred method to tackle this issue.

Kill a Process using the Xkill Command

The command xkill is used to kill processes on X servers without needing a PID or process name. It forcefully closes the application using the SIGKILL signal, so all the unsaved applications close without any saving.

Pass the below command in the terminal to start killing the applications.

$ xkill

After command execution, the normal cursor will turn into a cross arrow ❌.

Hover the cursor over the application you want to kill and press the left-click button as shown below.

Kill the application using xkill command
Kill the application using the xkill command

Above, you can see that, once the xkill command executed, the arrow cursor turned into a cross cursor. After that, I simply hovered over the Firefox browser and left-clicked to close.

You can also create a keyboard shortcut for this command to execute it using certain key combinations.

Summary

  • When the process freezes and becomes unresponsive, you can manually kill it using the command line.
  • To kill any process in Linux, first find out the PID or process name using the top, pidof, pgrep, ps commands.
  • Once you have found the PID or process name, use the kill, killall, or pkill commands to kill the process.
  • Didn’t you find anything? Then use the xkill command to kill any process with just one click.
  • If it is not working with the default SIGHUP or SIGTERM signals, then change it to the SIGKILL signal, which forcefully kills the process.
  • If it’s still not working, then make sure to check the original process owner or confirm you are a root user.

Force Kill Linux Process using Top, Kill, Pkill, Killall, and Xkill Commands

Author

Ubuntu Shell is an independent technology web portal with a heavy slant towards Linux, Server, Self-Hosting, DevOps, Cloud Learning, and Open Source.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.