Signals are software interrupts that are sent to programs to inform them that some important event has occurred. The events themselves might be requests from users or indications that a system problem (such as a memory access error) has occurred. The most commonly sent signals terminate a process. One called "SIGTERM" is sent whenever you issue a kill command with only its process ID as an argument. When you add a -9 (SIGKILL), you are sending a more forceful request that may not give the process in quetion any time to close files it has open or do any other type of housekeeping. Signals can be sent to processes using the kill command, but this is not the only way that this occurs. When you issue a ^c command to end a foreground process, you are sending a signal 2 (SIGINT) to the process. When you ^d to log out or exit a shell, you are sending a sending a signal 3 (SIGQUIT). Signal 1 (SIGHUP) sends a signal to a process requesting that it reread its configuration file, but keep running. This might be done, for example, when you've made changes to a web site's configuration file and want those changes to take effect, but you don't want a service outage. The following are some of the more common signals:
Signal Number Description SIGHUP 1 Hang up detected on controlling terminal or death of con- trolling process SIGINT 2 Issued if the user sends an interrupt signal (Ctrl + C) SIGQUIT 3 Issued if the user sends a quit signal (Ctrl + D) SIGFPE 8 Issued if an illegal mathematical operation is attempted SIGKILL 9 If a process gets this signal it must quit immediately and will not perform any clean-up operations SIGALRM 14 Alarm Clock signal (used for timers) SIGTERM 15 Software termination signal (sent by kill by default)
You can get a complete list of the signals available to you by using the kill -l command.
$ 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
The signals you will see will depend on the OS you are running, though the most common signals should be listed on all Unix systems whether Linux, Solaris, AIX, etc. What happens when a signal is sent depends on the signal and process, but every signal has a default action. Some of the most common responses are:
- terminate the process
- ignore the signal
- stop the process
- get a stopped process moving again
If you use the command kill -1 $$, for example, your login session will likely drop your connection. The kill -1 $$ command should terminate only your current shell. The kill -1 -1 command, on the other hand, will send SIGHUP to your entire process group and drop you on the floor ... rather expediently. DO NOT do this as root. On some systems (like HPUX), it will only log you (root) out; on others (like Solaris), it will reboot the system. Oops! When you use ^c within a terminal session, you can expect whatever process you are currently running to be terminated, leaving you back at the command prompt. However, there is an option that you can use to change what happens. You can issue a trap command that will change what happens when a signal is sent.
$ trap "echo no, I would rather not" 2 $ kill -2 $$ no, I would rather not
In this example, we are tying a different action to signal 2 and then sending that signal to the process associated with the current shell. You can also issue a command that ignores a particular signal.
$ trap '' 2 $ ./loop I'm still running I'm still running I'm still running ^c I'm still running
Start a process that loops indefinitely at this point and you can ^c your heart out. Your process won't die. But issue a ^z and you can still kill the suspended process with something like kill %1 which, of course, sends a SIGTERM, not a SIGINT.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.