Understanding Signals

by mike on December 30, 2010

Signals
Signals are asynchronous events that can occur to a running process and may be caused by hardware, software or users.    Signals are numeric integer messages that have been predefined so they understand what these signals mean.   When a process receives a signal, that process must respond to the signal.  Uncaught signals will cause default actions to take place, which often means the process is terminated.   If you use “kill -l”, or “trap -l” you can get a list of available signals.

kill -l
1) SIGHUP     2) SIGINT     3) SIGQUIT     4) SIGILL
5) SIGTRAP     6) SIGABRT     7) SIGBUS     8) SIGFPE
9) SIGKILL    10) SIGUSR1    11) SIGSEGV    12) SIGUSR2
13) SIGPIPE    14) SIGALRM    15) SIGTERM    16) SIGSTKFLT
17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP
21) SIGTTIN    22) SIGTTOU    23) SIGURG    24) SIGXCPU
25) SIGXFSZ    26) SIGVTALRM    27) SIGPROF    28) SIGWINCH
29) SIGIO    30) SIGPWR    31) SIGSYS    34) SIGRTMIN
35) SIGRTMIN+1    36) SIGRTMIN+2    37) SIGRTMIN+3    38) SIGRTMIN+4
39) SIGRTMIN+5    40) SIGRTMIN+6    41) SIGRTMIN+7    42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10    45) SIGRTMIN+11    46) SIGRTMIN+12
47) SIGRTMIN+13    48) SIGRTMIN+14    49) SIGRTMIN+15    50) SIGRTMAX-14
51) SIGRTMAX-13    52) SIGRTMAX-12    53) SIGRTMAX-11    54) SIGRTMAX-10
55) SIGRTMAX-9    56) SIGRTMAX-8    57) SIGRTMAX-7    58) SIGRTMAX-6
59) SIGRTMAX-5    60) SIGRTMAX-4    61) SIGRTMAX-3    62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX

Signals
0    EXIT     exit the program, not actual signal
1     HUP    hang up, will actually cause a daemon to reread the configuration file
2     INT   Interrupt or stop running, you can do this with Ctrl+C
3    QUIT  quit key, CTRL+SHIFT or CTRL+SHFT+\
9     KILL  stop immediately regardless of anything else, this is like an                 emergency kill switch
15    TERM  terminate nicely if at all possible
18    CONT  continue execution, this will start a stopped process
20    TSTP  stop executing, continue
DEBUG execute commands specified in trap statement
ERR     execute commands specified in trap statement after each command

The HUP signal either sends a reset signal to a daemon so that the daemon rereads the configuration information for the daemon or it is generated by the terminal driver to attempt to clean up processes that were related to a terminal.
INT is a signal that is sent by the terminal driver in a request to terminate the existing operation.
The KILL signal terminates the process at the kernel level.
The TERM is a signal to terminate execution and clean up and exit.
The TSTP signal is generated by the CTRL+Z command on the keyboard and requests a program to clean up their state and send themselves a STOP signal.
CONT signal starts a process that may have been stopped by a STOP signal.
How to Kill Processes
At times you may find processes that continue to run or are causing problems on the server. It is important to understand how to stop them. At times programs start but seem to hang when they really should be completed.  Use the kill command to stop these processes.   The kill command terminates a process.
kill [-signal] process_identifier (PID)
Note that the signal is the number or the symbolic name of the signal to be sent.  The best way to use kill is to use kill followed by the process PID or Process Identification Number. Here is an example:
kill 4578
You may have to use top or ps to find the process numbers you want to kill. This is the best way to stop a command.  Another way to do this is to use the -15  so it would look like this:
kill -15 4578
The default kill command uses -15 so that kill 4578 is the same as kill -15 4578.  The “-15” tells the process to stop just as if the user was logging out. The good thing here is that it will try to kill child processes as well.

The last alternative is the “-9″ option.  This will kill a process dead in its tracks.
kill -9 4578

{ 2 comments }

Smitty December 30, 2010 at 11:14 am

Nice article, but you should be aware that it’s up to the application developer to implement signal handlers. Almost all signals are ignored by default, the exceptions being obvious like SIGKILL. There is no guarantee that a daemon will re-read a config when given a SIGHUP.

BigDumbDinosaur December 31, 2010 at 5:40 pm

SIGHUP has been around for almost as long as UNIX and was intended to handle dialed-in connections via modem. If the server’s modem lost carrier without the user actually logging out, SIGHUP would be sent to his/her processes, causing them to terminate in an orderly fashion. The use of SIGHUP to tell a daemon to re-read its configuration file is a more recent development. Not all daemons do that–some will simply terminate the same as sending SIGTERM.

Comments on this entry are closed.

Previous post:

Next post: