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 easily 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.
😮 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 it cannot kill other users' processes.
- System-level processes like Display Manager cannot be killed by normal users.
- The root user is king, able to terminate anything in front of it (do not overdo it; it 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 the su
command.
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.
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 Name | Signal Value | Behavior |
---|---|---|
SIGHUP | 1 | Hangup |
SIGKILL | 9 | Kill Signal |
SIGTERM | 15 | Terminate |
- 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.
😮 Fact
Most of the time, the SIGKILL signal is the most effective way to kill any obstinate, unresponsive process.
Once you understand all of the signals used to kill any proces. The next thing required 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 by using the top
command.
The top
command lists all of the running processes in your system with their PID, memory, and CPU consumption.
- top
Below is the behavior of this command.
To kill any process within the top
interface, press the k keyboard button and enter the PID of the process as shown below.
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 the method mentioned.
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.
Above, you can find the PID for the nano editor, which is 7153, which can be used to kill it.
pgrep command
The pgrep
command is a complex way to find any PID. The basic work requires knowing the regular expression.
You can use pgrep
with a process name without any flags to return the PID, as shown below.
- pgrep nano
Below is the behavior of this command.
ps command
The ps
command is similar to the top
command, except without any interaction or fluctuations. Flags can be added to extract 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 processes handled using thesystemctl
command.
- ps aux
Below is the behavior of this command.
You can pipe the grep
command along with the ps
to output specific processes with the PID.
- ps aux | grep nano
Below is the behavior of this command.
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 on 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.
- Replace the PID with your process
- kill 7394
Below is the behavior of this command.
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.
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.
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.
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.
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.
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 xkill
command 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.
Above, you can see that, once the xkill
command was 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,
orpkill
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.