使用关闭按钮关闭终端与 Ctrl-D 的区别

ald*_*deb 7 shell terminal signals process-management background-process

当我启动后台进程然后使用窗口的关闭按钮关闭终端时,后台进程被杀死。但是,如果我使用Ctrl+关闭终端D,后台进程将继续运行:

sam@Sam-Pc:~$ yes > /dev/null &
[1] 10219
// I then close the terminal a reopen a new one
sam@Sam-Pc:~$ ps aux | grep yes 
sam      10295  0.0  0.0  15948  2152 pts/8    S+   00:54   0:00 grep --color=auto yes
Run Code Online (Sandbox Code Playgroud)

现在使用Ctrl+D关闭终端:

sam@Sam-Pc:~$ yes > /dev/null &
[1] 10299
sam@Sam-Pc:~$Ctrl-D
// I then reopen a new terminal
sam@Sam-Pc:~$ ps aux | grep yes 
sam      10219 99.4  0.0  11404   812 ?        R    00:52   2:01 yes
sam      10295  0.0  0.0  15948  2152 pts/8    S+   00:54   0:00 grep --color=auto yes
Run Code Online (Sandbox Code Playgroud)

有人可以解释这种行为吗?

谢谢!

hee*_*ayl 10

如果您使用关闭按钮关闭窗口,则外壳程序会向后台进程发送一个 SIGHUP 信号,该外壳程序也会在终端关闭时接收 SIGHUP 信号。进程的正常响应是退出,因此后台作业将被关闭。

另一方面,如果您按Cntl+ D,则不会发送任何信号,而是在 STDIN 上指示 EOF(文件结束)并且外壳(和终端)关闭。EOF 基本上意味着我们已经结束了 STDIN,没有什么可以输入的了。由于 EOF 不会触发任何与后台作业相关的响应,因此它们将继续进行。