bash所接收到的信号时,终端关闭 [英] Signals received by bash when terminal is closed

查看:126
本文介绍了bash所接收到的信号时,终端关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用陷阱捕获信号是这样的:

Use trap to capture signals like this:

i=-1;while((++i<33));
do
    trap "echo $i >> log.txt" $i;
done

和强制关闭终端。

在log.txt中的内容是那么(在RedHat Linux上):

The content in log.txt is then (under redhat linux):

1

18

1

17

从哪里这些信号?

推荐答案

第一个信号是SIGHUP;这被进程组发送到所有进程当终端断开(挂机 - 因此HUP)。

The first signal is SIGHUP; that gets sent to all processes in the process group when the terminal disconnects (hangs up - hence HUP).

第二个信号是SIGCONT(感谢,SiegeX,为数字)。这是令人惊讶的略;它表明你有一份工作中,不得不被允许再次运行背景停了下来。

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.

第三信号是另一SIGHUP。这是有可能发送,以确保持续的过程得到了它的转向退出,而是被送到了整个进程组。 (见 POSIX 信息标准的进程组,等等)。

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 POSIX standard for information on process groups, etc.).

第四个信号是SIGCHLD,这表明一个子进程死亡,尸体可用(好,状态可用)。

The fourth signals is a SIGCHLD, indicating that a child process died and the corpse is available (well, the status is available).

最终的信号,0,是指示它正在退出壳内部伪信号

The final signal, 0, is the shells internal pseudo-signal indicating that it is exiting.

您可以这样做:

trap 'echo Bye' 0

呼应再见的时候下以任何理由控制在shell退出。你选择了呼应信号向数字文件,而不是。由于外壳退出在这一点上,即是看到的最后一个信号的消息。因为shell死亡其父进程应该得到一个SIGCHLD信号。

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.

FWIW,MacOS X上10.6.7,我跑测试。没有在Mac OS X中的信号32,和某些映射是不同的,发送信号的顺序也不同:

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:

$ 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

在SIGINT首先是惊人的 - 我不认为我可以解释,除非它只是意味着某种不完全写(它应该已经阅读20,但SIGHUP造成了问题)。我不知道我能解释SIGCHLD信号要么;在SIGHUP和退出陷阱和以前一样的。

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.

在一定程度上,虽然信号是特定的系统 - 或者看起来是这样。该SIGHUP是常见的,不变的,虽然。

To some extent, though, the signals are system specific - or so it seems. The SIGHUP is common and constant, though.

这篇关于bash所接收到的信号时,终端关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆