Understanding the kill command, and how a kill command will work.

 

Linux Operating System comes with Kill command to terminate a process. Linux provides a mechanism to interrupt a running program. Here I am explaining how a kill command will work, also some basic signals that we are using on a daily basis.

Externally generated Interrupts:

SIGKILL – if Control-C doesn’t work and nothing else seems to work then SIGKILL tells the Linux Kernel to take a sledge hammer to to the process and force it down. It is possible for running processes to ignore other signals and keep running, but SIGKILL can’t be ignored.
SIGINT – the results of a Control-C which normally cancels a running program
SIGHUP – traditionally line hangup , but is used now for telling a process to reinitialize itself
SIGTERM – termination request — normal default when someone issues a command like: kill 1298

SIGTERM is the default and safest way to kill a process. SIGHUP is less secure way of killing a process as SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.

Signal Actions
While there are several actions for the various signals on a Linux system.

Term – This action is used to signal that the process should terminate
Core – This action is used to signal that the process should core dump and then terminate
Common Signals
A list of a few common signals, the numeric value of that signal, the action that is associated with it.

  • SIGHUP – 1 – Term

The SIGHUP signal is commonly used to tell a process to shutdown and restart, this signal can be caught and ignored by a process.

  • SIGINT – 2 – Term

The SIGINT signal is commonly used when a user presses ctrl+c on the keyboard.

  • SIGQUIT – 3 – Core

The SIGQUIT signal is useful for stopping a process and telling it to create a core dump file. The core file can be useful for debugging applications but keep in mind your system needs to be setup to allow the creation of core files.

  • SIGKILL – 9 – Term

The SIGKILL signal cannot be ignored by a process and the termination is handled outside of the process itself. This signal is useful for when an application has stopped responding or will not terminate after being given the SIGTERM command. This signal should stop more processes however there are exceptions, such as zombie processes.

  • SIGSEGV – 11 – Core

The SIGSEGV signal is generally sent to a process by the kernel when the process is misbehaving, it is used when there is an “Invalid memory reference” and you may commonly see a message such as segmentation fault in log files or via strace. You can also technically call this signal with kill as well; however it is mainly useful for creating core dump files, which can also be performed by using the SIGQUIT signal.

  • SIGTERM – 15 – Term

The SIGTERM signal is the default signal sent when invoking the kill command. This tells the process to shutdown and is generally accepted as the signal to use when shutting down cleanly. Technically this signal can be ignored, however that is considered a bad practice and is generally avoided.

How Kill work with signals.

Syntax:
kill -9 PID

When the kill command is run it is actually sending a singal to the process. By default the kill command will send a SIGTERM signal to the specified process.The SIGTERM signal tells the process that it should perform it’s shutdown procedure to terminate the process cleanly by closing all log files, connections, etc.

In general it is a good idea for applications to close open file handles and external connections during shutdown, however sometimes these processes can either take a long time or due to other issues not happen at all. Leaving the process in a state where it is not correctly running but also not terminated.

When a process is in a limbo state it is reasonable to send the process the SIGKILL signal, which can be invoked by running the kill command with the -9 flag. Unlike SIGTERM the SIGKILL signal cannot be captured by the process and thus it cannot be ignored. The SIGKILL signal is handled outside of the process completely, and is used to stop the process immediately. The problem with using SIGKILL is that it does not allow an application to close its open files or database connections cleanly and over time could cause other issues, therefor it is generally better to reserve the SIGKILL signal as a last resort.

 

Reference :
http://man7.org/linux/man-pages/man7/signal.7.html
http://space.wccnet.edu/~chasselb/linux275/ClassNotes/process/sigbasics.htm
http://bencane.com/2014/04/01/understanding-the-kill-command-and-how-to-terminate-processes-in-linux/

Leave a comment