FUNDAMENTALS A Complete Guide for Beginners
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]...
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)
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.
➍ 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.
Similar Readings
- The “killall” Command in Linux [6+ Practical Examples]
- The “jobs” Command in Linux [6 Practical Examples]
- The “ps” Command in Linux [9+ Practical Examples]
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.
➍ 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.
➏ 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.
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.
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.
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.
➍ 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.
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