Kill a Process in Linux Distribution

Stuck operating systems is a very common issue that almost every user has dealt at least once in their lifetime. We have all tried various methods as a solution. Few of them have proven successful and others not. One of the most effective one, if everything else fails, is to identify the process that has caused the system failure and then kill it. Killing a process is fairly easy if you already have some idea of how to manage system processes. First we have to identify the process that is sending harsh signals and if we can’t troubleshoot then it is better to terminate it. Word of caution – all the work that has not yet been saved will be lost once the process gets terminated. So, if things hang in balance analyze carefully before stepping forward.

Show currently running processes with ps command

Before killing a process, we need to understand what is currently running on our system. After running ps command in the terminal –

ps-command

It will show us the list of currently running processes that executed the command. Did you notice PID in the output? Each process is assigned a process ID by the kernel. So, here PID – 26464 is assigned to bash process and similarly PID – 26470 is assigned to the ps process.

There are other ways to identify the Process IDs of the running programs or processes. pidof is one such command. For example,

pidof <process-name>

This will provide us the process ID for the running program.

We are limiting ourselves here with basic introduction to the ps command. If you type ps aux command in the terminal, then you will get a more detailed output. This lists all the process that are running on our system including system-level processes.

Use kill command to control processes

We should first try to troubleshoot the process if it sends harsh signals. If it fails to respond then it is better to kill it. We will be using two commands to kill a process – kill and killall.

The use of kill command is fairly simple and all you have to do is run the following –

kill <PID>

with the kill command, we send the SIGTERM (15) signal wherein our process would terminate and free up the space. Our system wouldn’t be impacted negatively.

But, sometimes the process won’t respond to the kill SIGTERM (15) option. In such a scenario we need to use SIGKILL (9). Word of caution – it may create memory issues, corrupt our files etc. So, it should only be used when all other options have been ruled out. Run the following if you really want to –

kill -9 <PID>

Use killall command to control processes

Instead of providing the Process ID, we can directly use the name of the process to be killed. Also, it will kill all the processes which are associated with the Process name we provide. So, for SIGTERM (15), run the following –

killall <process-name>

If all options to terminate the process are exhausted, then use the command with the SIGKILL (9) option. But, the risks that are associated with the kill -9 option do exist with the killall -9 command too. So, opt for it after due consideration –

killall -9 <process-name>

In conclusion, we have touched upon ps, kill and killall commands. It is always better to assess what is causing your system to stuck. Once you identify the issue then, it is better to resolve the conflict. Killing a process should be opted only when all other options have been exhausted.