Home » Kill command

Kill command

What is a kill command?

Linux is an advanced and great operating system; however, it’s not perfect. A few applications might start behaving carelessly and become insensitive or begin consuming several system resources.

Insensitive applications can’t be rebooted because the actual application process never goes down completely. A single solution is for either restarting the system or killing the process of the application.

There are various utilities that permit us for terminating errant processes along with the kill command. This command is most widely used in Linux.

In Linux, the kill command (located inside the /bin/kill) can be defined as a built-in command.

It is used for manually terminating the processes. The behaviour of the kill command is slightly different among the shells and the /bin/kill standalone executable.

We can apply the type command for displaying every location on our system that is containing kill:

Kill command

The kill command transfers a signal towards a process in which further terminates the process. When the user does not describe any signal that is to be transferred with the kill command then the TERM signal (default) is transferred that will terminate the process. If the signal is not described, then it will default to -15 (-TERM).

The most widely applied signals are as follows:

  • HUP (1)- It reloads a process.
  • KILL (9)- It kills a process.
  • TERM (15)- It stops a process gracefully.

We can use the kill command along with the -l option for getting the list of every available signal:

Kill command

These signals could be described in three different ways. These ways are listed below:

  • By number (e.g. -6)
  • Without SIG prefix (e.g. -kill)
  • With SIG prefix (e.g. -SIGkill)

The below commands are similar to each other:

  • $ kill -SIGHUP PID_NUMBER
  • $ kill -HUP PID_NUMBER
  • $ kill -1 PID_NUMBER

The PIDs given to the kill command could be anyone from the below:

  1. The signal will be transferred to the process along with ID similar to the PID when PID is greater than zero.
  2. The signal will be transferred to every process inside the current process group when the PID is equivalent to zero. It means the signal will be transferred to every process related to the Shell GID that requests the kill command. We can apply the ps -efj command for viewing the IDs of the process group (GIDs).
  3. The signal will be transferred to every process along with the similar UID as a user requesting the command when the PID is equivalent to -1. If the requesting user is the root, then the signal will be transferred to every process except the kill and init process.
  4. The signal will be transferred to every process within the process group along with GID similar to the PID’s absolute value when the PID is smaller than -1.

Regular users are able to transfer signals to their processes. However, it does not include those users who are related to any other user. On the other hand, the root user is able to transfer the signal to the processes of the other user.

  • The PID of -1 is very unique because it indicates every process except init and kill, which is a parent process of every process over the system.
  • Negative values of PID are used for indicating the process group ID. When we pass any process group ID then every process inside that group will get the signal.
  • To show the running processes list, we can use the ps command and it will show us the running processes along with the PID number. We need to give the PID for specifying which process should get the signal of the kill command.

Syntax:

Kill command

ps aux: This command is used for displaying every running process along with their PIDs and other details. In case, we don’t know the process ID we can use this command.

Kill command

Options of the kill command

1. kill -l: This option is used for displaying every existed signal, we can apply the following command option:

Syntax:

Kill command

2. kill pid: This option is used for showing how to apply the PID along with the kill command.

Syntax:

Kill command

3. kill -L: The kill -L command can be used for listing the existed signals in a format of the table.

Syntax:

Kill command

4. Translate Signal Number: We can easily translate any signal number into a signal name by using the following command:

Kill command


You may also like