How to Kill a Process in Linux | Kill Command

Last Updated : 26 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn’t specify any signal that is to be sent along with the kill command, then a default TERM signal is sent that terminates the process.

Basic Syntax of kill command in Linux

The basic syntax of the `kill` command is as follows:

Syntax :

kill [signal] PID

Here, 

  • PID = The `kill` command requires the process ID (PID) of the process we want to terminate.
  • [signal] = We have to specify the signal and if we don’t specify the signal, the default signal `TERM` is sent to terminate the process

Signals can be specified in three ways:

Signals can be specified in three ways; they are as follows:

1. By number:

We can specify a signal using a number. For example, we have a PID `1212` and want to send a `SIGKILL` signal to kill this PID. SIGKILL has a signal number of `9` (To find signal numbers run `kill -l` command).

Syntax:

kill -9 1212

2. With SIG prefix (e.g/ -SIGkill)

We can also specify signal using SIG prefix. For example, we need to send a signal `SIGTERM` and PID is `1432`. To just check signal number of `SIGTERM` signal we can use `kill -l` command.

Syntax:

kill -SIGTERM 1432

3. Without SIG prefix:

We can also specify signals without using SIG prefix. For example, if want to send signal `TERM` and PID `1234`. To just check signal number of `TERM` signal we can use `kill -l` command.

Syntax:

kill -TERM 1234

Some Common Signals in kill Command

The table below shows some common signals and their corresponding numbers.

Signal Name Signal Number Description
SIGHUP 1 It hangup detected on controlling terminals or death of controlling process.
SIGINT 2 It interrupts from keyboard.
SIGKILL 9 It kills signal.
SIGTERM 15 It terminates signal.

To check signal name and number we can use `kill -l` command.

Options and Examples of kill Command in Linux

kill -l Option

To display all the available signals, you can use the below command option:

 Syntax:

kill -l
kill -l

kill -l

Note:

  • Negative PID values are used to indicate the process group ID. If you pass a process group ID then all the process within that group will receive the signal.
  • A PID of -1 is very special as it indicates all the processes except kill and init, which is the parent process of all processes on the system.
  • To display a list of running processes use the command ps and this will show you running processes with their PID number. To specify which process should receive the kill signal we need to provide the PID.

Syntax:

ps
ps

ps

kill PID Option

This option specifies the process ID of the process to be killed.

 Syntax:

kill pid

kill -s Option

This option specifies the signal to be sent to the process.

 Syntax:

kill {-signal | -s signal} pid 

Conclusion

The `kill` command in Linux is a very powerful utility for managing processes. We have understood the different ways to specify the signals and available options in the kill command which can help us to manage our system resources efficiently and resolve issues quickly and effectively.  



Next Article

Similar Reads