Javatpoint Logo
Javatpoint Logo

Kill Process Linux

In an operating system, there are many programs that take place on computer's RAM. These programs may be run by OS itself or a user, such programs are called 'Processes.' Usually, a process has its life cycle and get terminated by its own when it is completed or when we quit it manually. But, sometimes, a process may hang up due to error in process scheduling or because of consuming a lot of RAM or CPU. In such cases, we need to kill the processes manually to save our machine from unexpected hang up.

Linux allows various tools to kill an errant process. In order to kill a process, we must have the process information such as PID, signal, and more.

Locating the processes

To kill a process, we have to access the process information. There are various commands to track a process such as top, ps, pgrep, and pidof.

Linux system allows us to kill a process in various ways, such as kill a process by its name or process id (PID). So, we will use the above commands according to our need.

Locating the processes by 'top' command

The top command is used to list all the running processes in a Linux system. It displays detailed information such as PID, username, CPU usage, time, executed command, and more.

To locate the processes, execute the command as follows:

The above command will display the running processes. Consider the below output:

Kill Process Linux

We can browse the processes by scrolling up and down on the terminal. The top command provides various filters to filter the processes, such as process name, cpu usage, and more. To exit from the top section, press the 'q' key, it will return you to the terminal. However, we can also use the traditional exit option by pressing CTRL+ C keys.

Locating the processes by ps and grep command

The ps command is another way to display the process information. There are many options that are used with the ps command, such as aux, which means:

a : To display the processes for all the users

u : To display the processes used by particular user

x : To show all the processes. If we do not specify the x option, it will not display the GUI process.

Execute the below command to list all the running processes:

Consider the below output:

Kill Process Linux

The output of the ps command is similar to the top command. The process name and PID are given in the first two columns, and the process name is given on the very right column.

The ps command is more useful than the top command. It allows us to filter the output with grep command.

Suppose we want to filter all the process with the user name javatpo+, execute the command as follows:

The above command will filter all the specified process by grep command. Consider the below output:

Kill Process Linux

The above output provides the ps command as well as the grep command's functionality together.

Locating the process by pidof and pgrep command

The pidof command also allows us to locate the process. If we want to track a process by its name, the pidof command will be very useful. It displays the PIDs of the processes when it is used with the process name.

To locate the PID of a process, execute the pidof command as follows:

If we have the running process with exact name chrome, execute the command as follows to get it's PIDs.

consider the below output:

Kill Process Linux

Killing a Process

Now as we have tracked the processes, we can kill a process. There are various commands that are used to kill a process such as kill, kill, top, and pkill.

Before killing a process, it is necessary to know what processes we can kill. Below are some essential points about killing processes:

  • If you are a normal user, then you can only kill the processes that belong to you. You cannot kill the processes that are running by the other users. The top and ps aux commands display the users with the related process.
  • A root user can kill all the processes. However, we can also add sudo before any command to execute it as root.
  • In Linux, when a process is being killed, a special signal is transmitted to the processes. Although there are various types of signals in Linux, but in this case, Linux deals with the SIGKILLS and SIGTERM signals. By default. Linux sends the SIGTERM, which terminates the process gracefully. If you want to kill a process forcefully, use SIGKILL instead.
  • Linux keeps the status of the processes until it is completely removed from the system. So, the child processes will be displayed in the process list until a parent process is deleted.
  • We cannot kill a process if it is in 'uninterruptible sleep.' This situation occurs when a process performs an input/output operation. This state can be seen in the 8th column of top and ps aux command.

Killing a process by the killall command

The killall command is the easiest technique to kill a process if you know the exact process name, it is not running by any other user, and it is not in Z or D state. In kill all command, there is no need to locate the process or PID. To kill a process, execute the command as follows:

The above command will kill the process and quit the firefox browser. Consider the below output:

Kill Process Linux

If the command successfully kills the process, it will not give any output.

To forcefully kill the process, execute the killall command with -SIGKILL as follows:

we can also use -9 instead of -SIGKILL.

To kill a process interactively, execute the command as follows:

To kill a process as a different user, execute the command as follows:

We can also kill a process for a fixed time period by using -o and -y flags. To kill a process that has been running for more than 20 minutes, execute the below command: killall -o 20m process_name

To kill a process that has been running for less than 20 min, execute the below command:

Abbreviations for using the time period are as following:

seconds: s

minutes: m

hours: h

days: d

weeks: w

months: M

years: y

Kill a process by the pkill command

Sometimes we do not know the exact name of the process; in such case, the pkill command will be the most useful utility to kill a process. It allows us to kill a process by entering the matching name of the process. For example, we want to kill all the processes with matching name java, execute the command as follows:

it will close all the processes that are containing name java. Similarly for killing a firefox process, execute the command as follows:

Consider the below output:

Kill Process Linux

If the pkill command is successfully executed, it will not display any output.

To kill a process forcefully by pkill command, execute it as follows:

Kill a Process by the kill command

The kill command is the simplest utility to kill a process; all we need a PID of a process. Once we get the PID of the process, it is a straight forward process. To terminate a process, execute the kill command followed by PID. To locate the PID of a process, use the top or ps aux command, as explained above.

To kill a process having PID 5296, execute the command as follows:

To forcefully terminate a process, use the -SIGKILL or -9 option:

The above command will terminate the process having PID 5296.

Kill a Process by top command

The top command allows us to locate and kill the process. It is a straightforward process to terminate a process by using top command. First, execute the top command to locate the process and press the 'k' key while the command is running. It will dive you in the process killing mode, enter the PID of the process that you want to kill. Consider the below output:

Kill Process Linux

As we can see from the above output, there is an option " PID to signal/kill " to kill the process from the above the process list. Type the PID of the process and press ENTER key.

If we leave the PID as blank, it will terminate the topmost process.

Kill a Process by System Monitor

We can also terminate a process by Linux's graphical environment called system monitor. To kill a process through system monitor, follow the below steps:

Step1: Search or browse the system monitor, press enter to open it.

Step2: It will list all the running processes of your Linux system. Consider the below image:

Kill Process Linux

Step3: Browse the process and right-click on it that you want to terminate.

Step4: Select the Kill option. Also, we can use the CTRL+K keys to kill it.


Next TopicLinux terminating




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA