Killing Linux Processes

Killing Linux Processes with TERM Signals

Killing Linux Processes with TERM Signals

From time to time, if you are a Linux Administrator, you will have to kill a linux process.  The linux kernel has at least 32 signals.  Amongst those 32, you may recognize some of the more common ones:  SIGTERM, SIGHUP, and SIGKILL.  But what you may not know is that killing a process isn’t always a matter of killing a process, but signaling to the process to shut itself down or self-terminate.

Commands such as kill and killall send signals to running processes.  Signals can be specified using a the Signal Number or Signal Character Name.

Common Kill Signals

While there are 32 signals that can be used, below is a list of the more common signals that you may see:

Signal Code Number  Signal  Description
1 SIGHUP (hang up) – sent to a process whose controlling terminal is closed.  Typically, this means a ssh terminal is closed or GUI terminal  HUP is short for Hang Up.  This is the default term signal.
2 SIGINT (interrupt) – Terminal Interrupt Signal.  This is a signal sent to a process by its controlling terminal by the user who is running the process.  This is the same thing as using control-c.  Using SIGINT has the benefit of terminating the process as well as all other commands running in that terminal.
3 SIGQUIT (quit) – Terminal Quit Signal.  SIGQUIT is sent to a process by the controlling terminal while also requesting a core dump be performed.  SIGQUIT is the same as typing “Ctrl-\”.  The word “Quit” is echoed in the terminal running the process.
6 SIGABRT (abort) – Process Abort Signal.  SIGABRT is typically sent by the process itself to itself using the abort function of the C standard library, but it can also be sent to the process by other users or commands.  The word “Aborted” is echoed in the terminal when the signal is received.
9 SIGKILL (non-catchable, non-ignorable kill) – Kill unconditionally, Cannot be caught, blocked or ignored by the process.  Different than running SIGINT in that it kills that one process only but leaves other processes running in the same terminal running.  The word “Killed” is echoed in the terminal running the process.
14 SIGALRM (alarm clock) – Timer signal from alarm.  This signal is sent when time is up for a process to end.  Typically sent by a timer, or clock when allowed time has elapsed.  The text “Alarm clock” is echoed in the terminal when the signal is received.
15 SIGTERM (software termination signal) This is the default action if no term signal is listed.  When the SIGTERM signal is received by the process, it can be caught and interpreted or even ignored by the process.  The process can receive the SIGTERM signal and begin a proper termination, releasing resources and closing files properly.  SIGTERM is very similar to SIGINT.  The text “Terminated” is echoed in the terminal when the signal is received.

If you would like a list of all of the available signals on your system, you can list them using the fuser command:

# fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

Bash Kill Examples

Below are some examples of using kill commands with different signals:

Terminated Process Termination Command
$ i=1; while [ 1 -ne 2 ]; do sleep 200; echo $((i++)); done
Terminated
1
2(Note: the individual sleep is Terminated, but the loop continues.)
$ killall -15 sleep

(Send SIGTERM to all sleep processes)

$ i=1; while [ 1 -ne 2 ]; do sleep 200; echo $((i++)); done

$

 

(Note: the sleep process is killed as is the loop, returning us back to the bash prompt just as a ctrl-c would do.)

$ killall -SIGINT sleep

 

(Send SIGINT to all sleep processes)

$ i=1; while [ 1 -ne 2 ]; do sleep 200; echo $((i++)); done
Killed
1
2
$ kill -9 27098

(Send SIGKILL to process ID 27098)


Shutdown Processes Kindly

When you want to kill a process, it can be tempting to go all out with the “kill -9” command.  Kill -9, however, does not close files properly.  This creates some risk of damaging files, so it should be used as a last resort, using kinder methods first.

There are really two ways to shutdown a linux process –from inside the program or application itself or by telling it to terminate from the kernel.  Every application has a method designed for shutting it down.  You should try this method first.  If it is a GUI app, there may be a menu with a quit option.  If it is a service or daemon, you can try to shut it down with the service control manager.  You can read more about this in one of my past articles:  Booting, Changing Runlevels and Configuring Linux System Services.  The general idea is to using the “service <daemon name> stop” command to shutdown your process using the normal method.

After trying the nice ways to kill a command then you can start using signals starting with the nicer ones like SIGTERM or SIGINT and moving up to SIGKILL.  Be certain when using a nicer signal to give the process time to release resources and shutdown properly.  It may require a bit of patience.

Additional Resources

 

http://www.linux.org/threads/kill-commands-and-signals.4423/

http://en.wikipedia.org/wiki/SIGHUP#SIGHUP

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_01.html

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *