Linux KILL Command

Linux KILL Command

The kill command in Linux is used to terminate a currently running process. There are processes that exceed their run-time limit or get stuck and become unresponsive. The kill command is used to suspend (or

stop) such processes. The kill command sends a signal to the process which it wants to terminate. Depending upon what the user wants, different signals can be sent. The default signal that is sent by the kill command is TERM. The kill command enables the user to restart a process without restarting the whole machine

The syntax of the kill command is - 


If the <pid> equals 0, then the signal is sent to all the processes which are in the same process group as the calling process. If the <pid> is -1, then the signal is sent to each process for which the calling process has permission to send signals to. If the <pid> is less than -1, then the signal is sent to each process in the process group whose <pid> is equal to -pid.

The return type of the command depends upon whether it was a success or a failure. On success (i.e when at least one of the intended signals was sent), a 0 is returned. In case of a failure, 1 is returned and the errorno (error number) is set appropriately and 64 is returned when the kill command has partial success (when there are multiple target processes).

The errors that can occur are as follows -

1. EINVAL: An invalid signal was specified along with the command.

2.  EPERM: The calling process does not have the appropriate permissions to send signals to any of the target processes.

3. ESRCH: The target process (or the target process group) does not exist or has terminated even before the signal has reached it.

There are many options that are used with the kill command. A few of them are -

1. kill -l - The -l option is used to view the various signals that can be sent along with the kill command.

2. kill -s or kill --signal - The -s (or --signal) option is used to send a different signal (other than TERM)
along with the kill command.

Here, the [signal] part is used to send the desired signal and as seen above, the different signal options can be viewed using the -l option.

3. kill -L or kill -table - This option is similar to the -l option but it prints all the signals along with their corresponding numbers.

4. kill -p or kill --pid - It does not send the signal to the named process. It just returns the <pid> for that process.

5. kill --timeout - The --timeout option makes the kill command wait for a certain amount of time (in milliseconds) before sending the signal mentioned by the kill command.

6. kill --verbose - This option prints all the pid(s) of all the target processes along with the signal that will be sent to them.