Signals received with bash when terminal is closed
Use a trap to capture these signals:
i=-1;while((++i<33)); do trap "echo $i >> log.txt" $i; done And close the terminal by force.
Then the contents in log.txt (in redhat linux):
one
eighteen
one
17
0
Where are these signals from?
The first signal is SIGHUP; which is sent to all processes in the process group when the terminal shuts down (hangs - therefore, HUP).
The second signal is SIGCONT (thanks, SiegeX, for numbers). This is a bit surprising; this assumes that you had work stopped in the background that had to be allowed to start again.
The third signal is another SIGHUP. This was probably sent to ensure that the ongoing process got a turn out, but was sent to the entire process group. (See POSIX for information on process groups, etc.).
The fourth signal is SIGCHLD, indicating that the child process has died and the corpse is available (well, status is available).
The final signal, 0, is an internal shell pseudo signal indicating that it is coming out.
You can do:
trap 'echo Bye' 0 for the Bye echo when the shell goes out of control for some reason. Instead, you selected the signal number for the file instead. As the shell exits from this point, this is the last signal message that is visible. His parent process must receive a SIGCHLD signal because the shell has died.
FWIW, on MacOS X 10.6.7, I checked your test. There is no 32 signal on MacOS X, and some of the displays are different, and the sequence of signals sent is also different:
$ i=-1;while((++i<33)); > do > trap "echo $i >> log.txt" $i; > done -sh: trap: 32: invalid signal specification $ trap trap -- 'echo 0 >> log.txt' EXIT trap -- 'echo 1 >> log.txt' HUP trap -- 'echo 2 >> log.txt' INT trap -- 'echo 3 >> log.txt' QUIT trap -- 'echo 4 >> log.txt' ILL trap -- 'echo 5 >> log.txt' TRAP trap -- 'echo 6 >> log.txt' ABRT trap -- 'echo 7 >> log.txt' EMT trap -- 'echo 8 >> log.txt' FPE trap -- 'echo 9 >> log.txt' KILL trap -- 'echo 10 >> log.txt' BUS trap -- 'echo 11 >> log.txt' SEGV trap -- 'echo 12 >> log.txt' SYS trap -- 'echo 13 >> log.txt' PIPE trap -- 'echo 14 >> log.txt' ALRM trap -- 'echo 15 >> log.txt' TERM trap -- 'echo 16 >> log.txt' URG trap -- 'echo 17 >> log.txt' STOP trap -- 'echo 19 >> log.txt' CONT trap -- 'echo 20 >> log.txt' CHLD trap -- 'echo 23 >> log.txt' IO trap -- 'echo 24 >> log.txt' XCPU trap -- 'echo 25 >> log.txt' XFSZ trap -- 'echo 26 >> log.txt' VTALRM trap -- 'echo 27 >> log.txt' PROF trap -- 'echo 28 >> log.txt' WINCH trap -- 'echo 29 >> log.txt' INFO trap -- 'echo 30 >> log.txt' USR1 trap -- 'echo 31 >> log.txt' USR2 $ The signals captured in one pass were as follows:
2 1 20 0 In the second run, I got:
20 1 20 0 At first, SIGINT is surprising - I don't think I can explain it if it just means an incomplete record of some kind (it should have read 20, but the SIGHUP problem caused the problem). I'm not sure I can explain the SIGCHLD signals: the SIGHUP trap and the "exit" as before.
To some extent, however, the signals are system specific - or so it seems. SIGHUP is general and permanent, however.
If you ask what each signal is, use kill -l
$ 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 Note that a kill -0 <PID> does not return anything, but returns an exit code to indicate whether the signal can be sent to the PID