Javatpoint Logo
Javatpoint Logo

Kill Process

In this article, we will learn what the Kill Process is, why & where it is used. However, if you find this a bit confusing, let me explain it. The kill Process is nothing but a method to kill a process. Therefore, to get a basic idea about it before going ahead, it would be great to have a little discussion about it, which will also help you clear most of the doubts.

Introduction

In some cases, you may have noticed that when a Linux process becomes unresponsive or consumes a lot of resources, you are left with no option but to kill it. However, such type of situation is not always the case. Therefore, most processes have their own methods of shutting down to avoid these types of situations. Unfortunately, there are also some other types of processes that can malfunction and don't stop themselves. Thus, if a running background process becomes unresponsive or consumes more resources than it should, it becomes necessary to shut down or kill it using a process or method known as the "kill Process".

What Processes Can You Kill in Linux?

Before terminating or killing a process, you need to consider the permissions required to terminate a process, especially when you do not have access to the root account. However, if you have access to the root account or are the only one who owns the system, in that case, you have the power to terminate any kind of process at any time. Otherwise, a normal user who does not have root account access can use "sudo" in the prefix of the command while terminating a process using command line.

What happens when you kill a process?

Whenever a user kills a process, it actually sends a signal to the target process, considered a kill signal or termination message. However, there are several types of termination messages and some of them are as follows:

  • SIGKILL - It is generally considered as the ultimate way of killing a process. It will always kill a process and will kill the process abruptly, generating a fatal error. SIGKILL should always work. However, if the SIGKILL does not work, it clearly means that the operating system has been failed.
  • SIGTERM - It is generally considered a less effective method because it tries its best to kill a process, but unlike SIGKILL, it can be blocked or otherwise handled. Therefore, it can be considered a gentler way of attempting to terminate a process.

So, we can say that for most of the purposes, it will be best to use the methods that use the "SIGKILL" signal to kill a process in the safest & fastest way.

How to kill a process?

There are multiple ways to kill a process, but here we will see the most used and universal method you can use on any Linux-based operating system. Although, killing a process usually includes three main steps that we will see one by one.

Step 1: View Running Linux Processes

This is one of the most important steps to kill the process because this is the step in which we will discover which process is the target or consuming the most of the resources. Therefore to do that, you can use the following given command, which will display all the processes running on your system.

Syntax


Kill Process

Output

Kill Process

As you can see in the above given image, that this command reveals the process ID and users in addition to the amount of memory and CPU power used by each process.

Note: You can also kill a process directly from the "top" interface. All you need to do is press "k" on the keyboard and enter the process's id that you want to kill.

Step 2 Locate the process to Kill

In this step, we will learn how to locate a process because, in some cases, it is necessary to locate a process before we can kill it. However, there are several methods that can be used to locate a process. Still, here we will see the best one that always works as well as is easy to perform:

Locate process using "ps" command

The ps command also displays information about the processes similar to the "top" command that we have used earlier. However, it does not provide a similar interface as the "top" command does. Instead, the "ps" command provides a complete list of running processes, formatted based on the tags we add. To understand it in more detail, let us see its syntax.

Syntax

  • -a. it is used to view the processes of all users rather than just the current user.
  • -u. it is used to get detailed information about each of the processes

For example, executing the command ps -A displays a detailed process list of all processes.

Kill Process

Step 3 Terminating a Process

In general, there are several different methods of killing a process in Linux, depending on whether you know the name of the process running, the PID of the process, or just how long the process has been running. Let us see a command method of terminating a process using the process ID. In this method, we will use the "kill command.

Kill command

This command is used to kill processes by its PID( or process id). By default, it will send a "SIGTERM" signal that we have discussed above. However, this command can be used to kill multiple processes with a single command. The kill command can only kill a single process at a time with the given process ID. To kill a process, it usually sends a "SIGTERM "signal indicating a process to stop and waits for the program to run its shutdown routine.

Syntax

There are several options that can be used with the "kill" command. Some of the most commonly used are listed below:

  • -e. it used to find an exact match for the process name.
  • -I. it is used to ignore cases when trying to find the process name.
  • -i. it is used to ask for additional confirmation when killing the process.
  • -u. it is used to kill the only processes that a specific user owns.
  • -v. it is used to get a report on whether the process has been successfully killed.

Apart from killing processes by name, we can also use kill command to kill a certain process based on its age using the following command:

  • -o. Use this flag with a duration to kill all processes that have been running more than that amount of time.
  • -y. Use this flag with a duration to kill all processes that have been running less than that amount of time.

Lets us see how we can kill a process using the "kill" command. We can use this method if we know the correct id of the process.

Example

In this example, we will use the "ps" command to view the currently running process. After that, we will choose a process as the target, and by using the "kill" command with the process's name will try to kill a process. So let get into it.

Step1. Execute the following given command in the terminal as the root user:

Command

Output

Kill Process

Step2. Choose the process and remember its process id that you want to terminate. Here, I will choose the process name "calculator," and its process id is 3210, as you can also see in the given image:

Kill Process

Step3. After locating or knowing the process id of the process, we can kill or terminate the target process just by typing the process id next to the "kill" command as shown below:

Command

Output

Kill Process

As soon as you execute the above-given command, the calculator will be closed itself because its process is terminated. However, if you want to verify, you can also use the "ps -A" command to see all running processes, and you can search for the process with process id "3210". Although you will not find it anymore, as shown in the below-given image.

Kill Process

Key facts of Terminating a process

  • Whenever a process cannot be closed any other way, it can be manually killed via the command line.
  • To kill a process in Linux, we must first find the process. To do that, we can use any of the following given commands:
    1. top
    2. ps
    3. pidof
    4. pgrep
  • Once we find the process we want to kill, we can kill it using any of the following given commands:
    1. killall
    2. pkill
    3. kill
    4. xkill
    5. top
  • While killing a process, we can send a termination signal of SIGHUP, SIGKILL, or SIGTERM.
  • We need to have some permission to kill a process, which we can gain through the use of the sudo command.

Conclusion

In this article, we have shown and explained several ways to kill a process in Kali Linux. It is essential for all system administrators to learn and understand these Linux termination commands because who can know when you might need to deal with these types of situations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA