The “kill” Command in Linux [4+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The kill command in Linux is one of the handiest tools which can be used to terminate one or multiple processes from the terminal. It sends a signal to the process, which ultimately closes, terminates, or kills a particular process or group of processes. If the user doesn’t specify any signal to be sent along with the kill command, then the default TERM signal is sent that terminates the process. It is a handy tool for multitaskers who handle multiple programs simultaneously.

A. Description

The Kill command in Linux terminates any process manually. It is necessary when we get an unresponsive process.

B. Syntax

kill command is a built-in command that takes options, PID (process ID), or process name. The syntax of the kill command is as follows.

kill [OPTIONS]  [PID]...
Note: In the above syntax, OPTION and PID are enclosed by a square bracket, and after PID, there are three dots. These dots mean that you can pass one or multiple PIDs to the kill command.

C. Options

There are many options for the kill command. However, I have listed some most useful options. If you need any option that is not listed here, you can find that option on the man page (manual page) using the command below

man kill

Useful Options

  • -l (this option will show the available option of the kill command)
  • — SIGHUP -1 (this option will reload the process)
  • — SIGKILL, -KILL, -9 (this option will terminate the process)
  • — SIGTERM, -TERM -15 (this option will terminate the process)
Note: all of the options are case-sensitive. You must be careful while using these.

Practical Examples of the “kill” Command in Linux

Sometimes, our system crashes, and we can not close a process directly. In such a scenario, the kill command plays a magical role. A few real-life examples of the kill command are given below.

Example 1: Terminating a Process With the “kill” Command in Linux

After finding PID (process id) and using the PID, we can quickly kill a process. Here, I will first get the PID of the spotify application (which is currently running in the background) using the top and grep commands and then terminate the process using the kill command. I shall not give any signal here. And so, the default TERM signal will be passed with the kill command. To do so, you can follow the below procedure.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -n10 | grep spotify

➌ Now, press the ENTER button.

It will give you the PID of spotify in 1st column. So you need to pick the PIDs of the spotify application. The following image shows the PID of spotify on the terminal. Here 10834 10982 11009 11014 are the PIDs of spotify.

displaying PID of spotify on the terminal using top and grep command

➍ Type the following commands in the command prompt.

kill 10834 10982 11009 11014

➎ Now, press the ENTER button.

➏ Then type the following command in the command prompt.

top -n10 | grep spotify

Now, press the ENTER button.

Output >

The following image shows that the spotify application is closed now. This will not show you any PID of spotify as before if it is appropriately terminated.terminating firefox by passing PID s of spotify after the kill command. Then printing PID of spotify with kill command. but no PID is printed on the terminal because spotify is closed now.


Similar Readings


Example 2: Listing Available Signals & Using One of Them With the “kill” Command to Terminate a Process

After finding PID (process id) and using PID, we can quickly kill a process. Here, I will first get the list of available signals, then the PID of the firefox application using the top and grep commands, and then terminate the process using the kill command. To do so, you can follow the below procedure.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command to list the options of the kill command in the command prompt:

kill -l

Now, press the ENTER button.

It will display all available options or signals in the terminal to pass as the option of the kill command. You will pick the signal of SIGKILL, which is 9 in our instance.printing the list of available signal to pass with kill command.

➍ Type the following command in the command prompt:

top -n3 | grep firefox

Now, press the ENTER button.

It will give you the PID of firefox in 1st column. So you need to pick the PID of firefox. Here 11118 is the PID of firefox. The following image shows the PID of firefox.printing the PID of firefox on the terminal using top and grep command.

Type the following command in the command prompt.

kill -9 11118

➐ Now, press the ENTER button.

Again, type the following command in the command prompt.

top -n3 | grep firefox

Now, press the ENTER button.

Output >

In the following image, you can see the firefox application is closed, and no PID of firefox is displayed in the terminal.terminating the firefox by using -9 as option and PID of firefox after kill command. then top and grep command is used to print the PID of firefox on the terminal, but no PID is displayed hence firefox is terminated

Example 3: Terminating a Process by Its Name Using the “kill” Command in Linux

Sometimes it is inconvenient to use PID from the top command. To overcome such a situation, you can use the pidof command to search the PID of a process and pass the PIDs to the kill command in the same command line. Here, I will first get the PID of the firefox application using the pidof command and pass the PIDs of the firefox to kill command along with an option in the same command line. It will terminate the firefox applications. To do so, you can follow the below procedure.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

kill -9 $(pidof firefox)

❸ Now, press the ENTER button.

➍ Type the following command in the command prompt:

pidof firefox

➎ Now, press the ENTER button.

Output >

The following image shows that the firefox application is terminated since no PID is displayed in the terminal.primarily printed all PIDs of firefox, then terminate the firefox with kill command passing -9 as option and PIDs of firefox using dollar sign.

Alternatively, Terminating Process by Filename (Without PID) Using the “killall” Command

To skip the job of grabbing the PID of the process while killing any process, you can use the killall command. Here, I will use the killall command and pass firefox as an argument with the -9 options to kill the firefox application. Eventually, it will terminate the firefox application. To do so, you can follow the below procedure.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

killall -9 firefox

❸ Now, press the ENTER button.

Then type the following command

top -n3 | grep firefox

Now, press the ENTER button.

Output >

In the image below, there is no PID of firefox. Therefore firefox is appropriately terminated.terminating firefox application using killall command accompanied by -9 as option and firefox as argument. Then PIDs of firefox are tried with the combination of top and grep command. no PID of firefox is printed hence firefox is closed now.

Example 4: Terminating Multiple Processes With the “kill” Command in Linux

Here, I will first get the PID of the spotify and firefox applications using the top and grep commands and then terminate the process using the kill command and the -9 options. To do so, you can follow the below procedure.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -n5 | grep -e spotify -e firefox

➌ Now, press the ENTER button.

It will give you the PID of firefox and spotify. So you need to pick the PID of firefox and spotify. The following image shows the PID of spotify on the terminal. Here 4463, 4794, 4977, and 4936 are the PIDs of firefox and spotify respectively.printing the PIDs of spotify and firefox on the terminal using the combination of top and grep command.

 Type the following commands in the command prompt.

Kill -9 4463 4794 4977 4936

Type the following command in the command prompt:

top -n5 | grep -e spotify -e firefox

Now, press the ENTER button.

Output >

In the following image, you can see that the firefox and spotify application is closed now. The terminal will not show you any PID of firefox and spotify as before if they are appropriately terminated.firefox and spotify applications is terminated using kill command passing -9 as options and PIDs of firefox and spotify as argument. then combination of top and grep command is used to print ta PIDs of firefox and spotify but no PID is displayed hence firefox and spotify is closed now.

Conclusion

In this article, I have tried to demonstrate the applications and effectiveness of the kill command in Linux. I hope you’ll be competent enough to explore more things with the help of these practical examples and kill any process in case of unresponsive situations.


Similar Readings

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment