bash 终端关闭时bash接收到的信号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5546223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Signals received by bash when terminal is closed
提问by fireworks2
Use trap to capture signals like this:
使用 trap 来捕获这样的信号:
i=-1;while((++i<33));
do
trap "echo $i >> log.txt" $i;
done
And close the terminal by force.
并强行关闭终端。
The content in log.txt is then (under redhat linux):
那么log.txt中的内容就是(redhat linux下):
1
18
1
17
0
1
18
1
17
0
Where these signals from?
这些信号来自哪里?
采纳答案by Jonathan Leffler
The first signal is SIGHUP; that gets sent to all processes in the process group when the terminal disconnects (hangs up - hence HUP).
第一个信号是SIGHUP;当终端断开连接(挂断 - 因此 HUP)时,它会被发送到进程组中的所有进程。
The second signal is SIGCONT (thanks, SiegeX, for the numbers). This is slightly surprising; it suggests you had a job stopped in the background which had to be allowed to run again.
第二个信号是 SIGCONT(感谢 SiegeX 提供数字)。这有点令人惊讶;它表明您在后台停止了一项必须允许再次运行的作业。
The third signal is another SIGHUP. This was likely sent to ensure that the continued process got its turn to exit, but was sent to the whole process group. (See the POSIXstandard for information on process groups, etc.).
第三个信号是另一个 SIGHUP。这可能被发送以确保继续的进程轮到退出,但被发送到整个进程组。(有关进程组等的信息,请参阅POSIX标准)。
The fourth signals is a SIGCHLD, indicating that a child process died and the corpse is available (well, the status is available).
第四个信号是SIGCHLD,表示子进程死了,尸体可用(嗯,状态可用)。
The final signal, 0, is the shells internal pseudo-signal indicating that it is exiting.
最后一个信号 0 是 shell 内部伪信号,表明它正在退出。
You can do:
你可以做:
trap 'echo Bye' 0
to echo 'Bye' when the shell exits under control for any reason. You chose to echo the signal number to the file instead. Since the shell exits at this point, that is the last signal message that is seen. Its parent process should get a SIGCHLD signal because the shell died.
当外壳出于任何原因退出控制时,回显“再见”。您选择将信号编号回显到文件中。由于此时 shell 退出,这是看到的最后一个信号消息。它的父进程应该得到一个 SIGCHLD 信号,因为 shell 死了。
FWIW, on MacOS X 10.6.7, I ran your test. There isn't a signal 32 on MacOS X, and some of the mappings are different, and the sequence of signals sent is also different:
FWIW,在 MacOS X 10.6.7 上,我运行了您的测试。MacOS X 上没有信号 32,部分映射不同,发送信号的顺序也不同:
$ 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 run were:
在一次运行中捕获的信号是:
2
1
20
0
In a second run, I got:
在第二次运行中,我得到:
20
1
20
0
The SIGINT first is surprising -- I don't think I can explain that unless it simply means an incomplete write of some sort (it should have read 20 but the SIGHUP caused a problem). I'm not sure that I can explain the SIGCHLD signals either; the SIGHUP and 'exit' trap are as before.
SIGINT 首先是令人惊讶的——我认为我无法解释这一点,除非它只是意味着某种不完整的写入(它应该读取 20 但 SIGHUP 引起了问题)。我也不确定是否可以解释 SIGCHLD 信号;SIGHUP 和 'exit' 陷阱和以前一样。
To some extent, though, the signals are system specific - or so it seems. The SIGHUP is common and constant, though.
不过,在某种程度上,信号是系统特定的——至少看起来是这样。不过,SIGHUP 是常见且恒定的。
回答by SiegeX
If you're asking what each of the signals are, use kill -l
如果您要问每个信号是什么,请使用 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 nothing other than return an exit code to indicate if a signal can be sent to the PID
请注意,kill -0 <PID>除了返回退出代码以指示是否可以将信号发送到 PID 之外,a不会执行任何操作

