Linux Commands

Linux kill command

The kill is a very useful command in Linux that is used to terminate the process manually. It sends a signal which ultimately terminates or kills a particular process or group of processes. If the user does not specify a signal to send with the kill command, the process is terminated using the default TERM signal.

Getting the processes information

Getting the unresponsive process is the first step in killing it. We can get the process information by using two commands, i.e., top and ps. To see a list of your running processes, type top from the command line:

$ top

Or

$ ps

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image3 final.png

You will find some relevant details on the terminal. For instance, imagine a situation where the “Application” related process has become unresponsive. In our case, there are four instances of Applications running with Process IDs (PID) 5441, 3870, 5062, and 3637, 4233, 770, 3787, according to our top command display.

Although the top is extremely useful, it is not always the most efficient way to obtain information. Now let us say you need to kill the application processes, but you don’t want to get the real-time information from the top command. in this scenario, you can use the ps command. Furthermore, we can filter the output using the grep command. We are only interested in listing down the Application-related processes. Use the command given below to see the application related processes:

$ ps aux | grep application

Whereas,

a = Show all user-related processes

u = Show the process’s the owner/user.

x = Show all the processes which are not terminal related

The grep prints lines that match a pattern, while ps returns a snapshot of a current process. The reason for using grep to filter ps is simple: if you run ps by itself, you will get a snapshot of all running processes.

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image5 final.png

Display list of Signals:

Type kill -l to display the list of all signals. We can send various signals with the kill command.

We can choose any signal from the list of given signals.

$ kill –l

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image4 final.png

There are some common kill signals:

Signals name Signal value Effect

SIGHUP 1 Hang up

SIGINT 2 Interrupt from the keyboard

SIGKILL 9 Kill Signal

SIGTERM 15 Termination signal

SIGSTOP 17, 19, 23 Stop process

Kill processes by ID:

So, let’s use the kill command to terminate our Application. The syntax would be like this:

Kill signal PID

Send the kill signal to kill 4129 processes:

Kill -9 4124

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image7 final.png

Send the signal to hang up the 5427 processes:

Kill -1 5427

Send the signal to interrupt the process 5250 from the keyboard:

Kill -2 5250

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image2 final.png

Kill process by name:

If you want to kill all the processes running in the application. Use killall command:

$ killall -9 application

D:\Aqsa\5 march\Linux kill command\Linux kill command\images\image1 final.png

All the processes running in an application have been killed successfully.

Conclusion:

Before killing any Linux processes, first, we need to see the list of all running processes to get the process ID. We can use multiple signals with the kill command, whereas each signal is used for a specific purpose.

About the author

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.