Force Kill process in Linux

Ever came across a situation where you run an application and suddenly it stops responding? The most reliable thing to do in such a case would be to shut down the running application and re-start the process, right? But what if you’re not able to kill the running process completely! In such a scenario, you have to force kill process. 

Here is a quick guide on how to force kill a process in Linux

There are various ways for processes to shut down. It is also possible for processes to go wrong. Using a command to kill a background process becomes necessary if a process is unresponsive and refuses to stop. We’ve created this comprehensive guide on killing a Linux process using the command line.

How to kill a Linux process

To force kill the Linux process, you first need to check which processes are currently running in the background.

You can get a comprehensive view of the currently running processes by running the top command in Linux. Just type in “top” (without double quotes) in the command line and you will be able to see a real-time dynamic view of all the running processes in the system. This command normally displays details about your system, including a list of all the processes the kernel is managing at present. the top will display both user names and process IDs, as well as what memory and CPU power each process is using.

You can exit the top interface by pressing q.

To kill a process in the top interface itself, press k and enter the process Id.

What Processes Can You Kill in Linux?

Before killing or terminating a process, you need to check and, in some cases, also need to change the file permissions provided to you if required. It is possible to kill all processes as a root. If you want to run a command as root, you can add sudo before it or use su to get a root shell. After that, run the command.

Upon terminating a process, it receives a termination message. Termination messages take many forms, such as:

  1. SIGKILL: The ultimate way to kill a process is by sending a SIGKILL. Yes, you heard it right. SIGKILL is a message that never fails in killing a process. Whenever you execute it, a process will be killed with an abrupt end, resulting in a fatal error. It is always recommended to use SIGKILL. You have a failed operating system if it does not work!
  2. SIGTERM: SIGTERM attempts to terminate a process, but unlike SIGKILL, its execution is not guaranteed. One can think of it as a gentler way of stopping a process.

Consequently, when it comes to terminating processes, SIGKILL will most often prove the fastest and most efficient method.

Locating the process:

First of all, you need to locate the process in order to kill the process. There are numerous ways in which one can search for processes in Linux. It is possible to find processes through either their name (or a partial name) or, more commonly, by their process ID (also known as a pid).

We will look at both methods here.

ps command

You can locate a process using top command as mentioned earlier and you will be able to see important information like PID. While top is extremely useful, it isn’t always the best way to find the information you’re seeking. For instance, if you know that the Internet-explorer process or opera process or any other specific process needs to be killed, then you don’t need to glance through the top window’s real-time information. In this case, grep can be used to filter the output of the ps command. A process snapshot is displayed by ps, and the pattern matching output is displayed by grep. That’s all you need! Note that by using grep to filter the output of ps, we avoid getting a snapshot of current processes that would otherwise generate a long list.

Our only concern is that the listing is related to opera. Therefore, the following command would be used:

ps aux| grep opera

‘aux’ options:

  • -a: Rather than viewing only the current user’s process, you can view those of all users.
  • -u: Describe the processes in detail
  • -x: You can also include processes controlled by daemons instead of users.

Knowledge box: Getting information regarding a graphical application is straightforward when you use the -x option.

pgrep command

Syntax:

pgef <choice> <pattern>

This command will return processes that match a specific selection criterion known as a pattern.

The following choices are available with this command:

  • -l: Both the process name and the PID are listed.
  • -n: Return the most recent process.
  • -o: The oldest process will be returned.
  • -u: Identify processes for specific users only.
  • -x: Match only processes whose characteristics exactly match the pattern.

A list of all processes owned by root can be viewed with the command pgrep -u root. Processes owned by root beginning with “a” will be returned by pgrep -u root ‘a*’.

Completely end Linux processes.

Two pieces of information turn out to be helpful while killing the Linux process.

You can either kill a process by process name or kill a process by process ID.

When it comes to Linux, several commands are available with different properties to kill the Linux process.

Here we will go through three commands used to terminate the Linux process:

  1. kill 
  2. killall
  3. pkill

The primary difference between all these commands is that While kill terminates running processes based on their Process ID (PID), killall and pkill terminate them in response to the process name and other attributes. Keep in mind that normal users cannot kill other users’ processes, but the root user can.

Kill process by pid Linux: Using kill to terminate processes

First, you must find the process PID before executing the kill command. There are various commands for doing so, including top, ps, pidof, and pgrep. If, for example, the Chrome browser has become unresponsive, and you must kill the browser process, here’s what you would do. The pidof command can be used to determine the process ID as follows:

pidof chrome

All Chrome processes will be printed with this command as follows:

3827 11679 2119 3919

Send the TERM signal to terminate all Chrome processes:

kill -9 3827 11679 2119 3919

NOTE: With kill, killall, and pkill, a signal is sent to a selected process or process group. Every tool sends 15 (TERM) when the signal is not provided.

In most cases, the following signals are used:

  • 1/ -HUP: to reload a process
  • 9 / -kill: to kill a process
  • 15 / -term: to gracefully stop a process

Kill process by name Linux: Using killall to terminate processes

First, you must know the process name before executing the killall command.

killall -9 chrome

Several options are available when executing killall command, such as sending signals to processes owned by a particular user, matching a process’s name against regular expressions, and retrieving a process’s creation time. Type killall (without any arguments) on your terminal to see all available options.

When the following command is run, all processes run by the user “justin” are terminated:

Sudo killall -u justin

Using pkill to terminate processes

When called with a pattern, pkill terminates processes matching that pattern:

pkill -9 chrome

NOTE: An exact match isn’t required between the process names! Just like killall, the pkill command also allows you to send a signal to processes owned by a particular user.

pkill -9 -u justin chrome

Conclusion

We hope by now terminating an unresponsive program is easy peasy for you. It is easy to end nonresponsive programs with the killkillall, and pkill commands. Just know the process name or PID and a few easy tricks!

Leave a Comment

Your email address will not be published.