Shell – Difference between closing the terminal using the closing button, and Ctrl-D

background-processprocess-managementshellsignalsterminal

When I'm launching a background process and then closes the terminal using the window's closing button, the background process gets killed. However if I close the terminal using Ctrl+D, the background process keeps running:

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

And now using Ctrl+D to close the terminal:

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

Could anybody explain this behaviour?

Thanks!

Best Answer

If you close the window using closing button, then a SIGHUP is sent to the background processes by the shell which also receives SIGHUP as the terminal closes. The normal response of the processes would be to exit, so the background jobs will be closed.

On the other hand if you press Cntl + D, no signal is sent rather an EOF (End of File) is indicated on the STDIN and the shell (and terminal) closes. EOF basically means that we have reached an end to the STDIN, there is nothing more to input. As EOF does not trigger any response related to background jobs they will keep on continuing.

Related Question