Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it safe to close shell just by clicking "X" or using "ctrl+d" while logged in as root with su or is it safer to close it by typing "exit"? Is there any difference (security-wise) at all or is it the same and it does not matter which way you use?

share|improve this question
add comment

3 Answers

up vote 10 down vote accepted

Ctrl-D or exit is the same.

If you using the "X", the window manager sends a terminate signal to the shell. Which has at the end the same result. If the shell does not react, the user usually gets a warning and can then force the shell to end (equal to kill -9 $PID).

There is one small difference between the "X"- and exit-method if you are using zsh as shell: If you have a background process running, exit/ctrl-D produces the first time only a warning message "you have running jobs", the second time the shell is closed. With the "X" button, the shell terminates immediately without any warnings.

share|improve this answer
3  
Also there is a difference in bash as well. If you ctrl+d, then any processes backgrounded with & will remain running. If you "X", then they'll get terminated. –  Patrick Mar 10 '12 at 18:58
2  
@Patrick this seems to depend on the terminal emulator rather than the shell. Unless the emulator explicitly kills backgrounded processes, as far as I am aware bash won’t send them SIGHUP no matter how you exit. I just tried this on Mac OS X 10.7.3. Terminal doesn’t kill the backgrounded processes, but Xterm (running in X11) does. I don’t know whether that’s a behavior of Xterm specifically or X11 generally. –  Chris Page Mar 10 '12 at 20:20
    
@jofel this is not true, see my answer. –  Patrick Apr 26 '12 at 13:37
    
@Patrick thanks for pointing this out. I'll edit or delete my answer. –  jofel Apr 26 '12 at 13:42
    
@jofel nah, dont delete, the zsh info is still useful. I'm only a noob at zsh, so I cant comment there. But that does bring up a good point, so I edited my answer to note that different shells can behave differently, and the default behavior I described is bash dependent, while the OP doesnt specify his shell. –  Patrick Apr 26 '12 at 13:54
add comment

Closing the terminal emulator is not the same as pressing CTRL + D (unless the option huponexit is set).

  • When you close the terminal (clicking "X"), the terminal emulator sends a SIGHUP to the shell. When bash receives a SIGHUP, it also sends a SIGHUP to all your jobs.

  • When you exit the terminal with CTRL+D or logout or exit, the shell exits without sending a SIGHUP to all the jobs. This is the default behavior, but can be changed by setting the bash shopt setting huponexit.

So ultimately if your goal is to make sure there's nothing running in the background, its safter to click "X".

.

The info provided by jofel in his comment is incorrect (sorry jofel). The terminal emulator has no clue what jobs are running under the shell, thus it cant terminate them.

NOTE:
This behavior is completely dependent upon the shell. It is entirely possible that different shells have different default behaviors.

share|improve this answer
add comment

There is no difference. So much that, when you press ^D, you are doing the same thing as typing 'exit':

$ su -
Password:
# [press ^D]
logout
share|improve this answer
    
Yes, CTRL+D and exit are the same, the question was about clicking the window manager's "X" to close the window, not typing exit :-) –  Patrick Apr 26 '12 at 13:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.