关闭和退出终端有什么区别

关闭和退出终端有什么区别

exit我正在研究终端退出时发出的信号。但是,我发现输入退出终端和直接单击 x 按钮之间存在差异。

输入Exit退出终端

1号航站楼:

$ echo $$
31461

2 号航站楼:

$ sudo strace -e signal -p 31461
strace: Process 31461 attached

然后我exit在终端 1 中输入。终端 2 输出以下消息:

rt_sigaction(SIGINT, {sa_handler=0x564317c26b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c7d110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x564317c265e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c7d110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x564317c26ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c7d110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x564317c26ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c7d110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=0x564317c265d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c7c570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x564317c26b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c26b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x564317c26b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, {sa_handler=0x564317c26b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa7c44fe090}, 8) = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
+++ exited with 0 +++

点击 x 按钮

1号航站楼:

$ echo $$
31519

2 号航站楼:

$ sudo strace -e signal -p 31519
strace: Process 31519 attached

然后我点击 x 按钮关闭了终端。终端 2 输出以下消息:

--- SIGHUP {si_signo=SIGHUP, si_code=SI_USER, si_pid=3265, si_uid=1000} ---
rt_sigreturn({mask=[]})                 = -1 EINTR (Interrupted system call)
rt_sigaction(SIGINT, {sa_handler=0x55c2f3b43b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x55c2f3b435e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=0x55c2f3b435d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b99570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
kill(31519, SIGHUP)                     = 0
--- SIGHUP {si_signo=SIGHUP, si_code=SI_USER, si_pid=31519, si_uid=1000} ---
rt_sigreturn({mask=[]})                 = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, [HUP INT QUIT ALRM TERM TSTP TTIN TTOU], [], 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b435e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGQUIT, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
--- SIGCONT {si_signo=SIGCONT, si_code=SI_KERNEL} ---
rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTSTP, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTSTP, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTOU, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTOU, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTIN, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTIN, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
--- SIGHUP {si_signo=SIGHUP, si_code=SI_KERNEL} ---
rt_sigreturn({mask=[]})                 = 0
rt_sigaction(SIGWINCH, {sa_handler=0x55c2f3b99570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b435d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x55c2f3b43b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x55c2f3b435e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=0x55c2f3b435d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b99570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
kill(31519, SIGHUP)                     = 0
--- SIGHUP {si_signo=SIGHUP, si_code=SI_USER, si_pid=31519, si_uid=1000} ---
rt_sigreturn({mask=[]})                 = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigprocmask(SIG_BLOCK, [HUP INT QUIT ALRM TERM TSTP TTIN TTOU], [], 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b435e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGQUIT, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTSTP, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTSTP, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTOU, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTOU, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTIN, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTTIN, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=0x55c2f3b99570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b435d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGINT, {sa_handler=0x55c2f3b43b00, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGTERM, {sa_handler=0x55c2f3b435e0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGALRM, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b9a110, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGWINCH, {sa_handler=0x55c2f3b435d0, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b99570, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f26443d5090}, 8) = 0
rt_sigaction(SIGHUP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, {sa_handler=0x55c2f3b43ed0, sa_mask=[HUP INT ILL TRAP ABRT BUS FPE USR1 SEGV USR2 PIPE ALRM TERM XCPU XFSZ VTALRM SYS], sa_flags=SA_RESTORER, sa_restorer=0x7f26443d5090}, 8) = 0
kill(31519, SIGHUP)                     = 0
--- SIGHUP {si_signo=SIGHUP, si_code=SI_USER, si_pid=31519, si_uid=1000} ---
+++ killed by SIGHUP +++

消息第一行显示的 pid 3265 是关于 gnome-terminal-server 的

$ ps aux | grep 3265
kartone     3265  1.0  1.1 1013496 46336 ?       Ssl  11:23   0:13 /usr/libexec/gnome-terminal-server
kartone    31602  0.0  0.0  11984   720 pts/1    S+   11:45   0:00 grep --color=auto 3265

main bash

默认情况下,shell 在收到 SIGHUP 后退出。在退出之前,交互式 shell 会向所有正在运行或已停止的作业重新发送 SIGHUP。已停止的作业会收到 SIGCONT 以确保它们收到 SIGHUP。要防止 shell 向特定作业发送信号,应使用 disown 内置命令将其从作业表中删除(请参阅下面的 SHELL 内建命令),或使用 disown -h 将其标记为不接收 SIGHUP。

点击x按钮的方法好像对应于man bash因为SIGHUP,所以消息中出现了SIGCONT。

我在网上搜索后发现

  1. 退出终端 vs 关闭终端答案是模棱两可的。
  2. 以图形方式退出和关闭终端有什么区别?

关闭窗口而不是输入 exit 会发送 SIGHUP。

但是,这意味着输入 exit 无法发送 SIGHUP,这与man bash

两种退出终端的方法输出的信息不完全相同,所以我有疑问:

  1. 为什么输出的信息不一样?
  2. 这两种方法中 shell 发送的信号是否不同?
  3. 这两种方法还有什么区别?

购物

$ shopt -p huponexit
shopt -u huponexit

操作系统:VMWare 的 Ubuntu 20.04LTS

相关内容