Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

So, say I get disconnected from an SSH-session after I've started rsync or cp or any other command that can be long running. Does that command keep running until it's finished after I get disconnected or does it just get killed?

Always wondered this.

share|improve this question
    
I just want to add to what has been said above that, if you find yourself in a situation when you need to put an already running process into screen, try reptyr. –  a sad dude Jan 7 '13 at 19:00

3 Answers 3

up vote 57 down vote accepted

They will get killed, but not necessarily immediately. It depends on how long it takes for the SSH daemon to decide that your connection is dead. What follows is a longer explanation that will help you understand how it actually works.

When you logged in, the SSH daemon allocated a pseudo-terminal for you and attached it to your user's configured login shell. This is called the controlling terminal. Every program you start normally at that point, no matter how many layers of shells deep, will ultimately "trace its ancestry" back to that shell. You can observe this with the pstree command.

When the SSH daemon process associated with your connection decides that your connection is dead, it sends a hangup signal ("SIGHUP") to the login shell. This notifies the shell that you've vanished and that it should begin cleaning up after itself; what happens at this point is shell specific (search its documentation page for "HUP"), but for the most part it will start sending SIGHUP to running jobs associated with it before terminating. Each of those processes, in turn, will do whatever they're configured to do on receipt of that signal. Usually that means terminating. If those jobs have jobs of their own, the signal will often get passed along as well.

The processes that survive a hangup of the controlling terminal are ones that either disassociated themselves from having a terminal (daemon processes that you started inside of it), or ones that were invoked with a prefixed nohup command. (i.e. "don't hang up on this")

Terminal multiplexers are a common way of keeping your shell environment intact between disconnections. They allow you to detach from your shell processes in a way that you can reattach to them later, regardless of whether that disconnection was accidental or deliberate. tmux and screen are the more popular ones; syntax for using them is beyond the scope of your question, but they're worth looking into.

share|improve this answer
1  
I'd also add the 'dtach' command to that list - it's sort of the opposite of screen/tmux in that it lets you attach multiple terminals to a single session, and is also great for prolonging a session, although it doesn't provide any means of replaying the recent history. –  fluffy Jan 6 '13 at 17:10
    
dtach url is here: dtach.sourceforge.net –  slm Jan 6 '13 at 20:17
    
excellent explanation. I feel more linuxey already! –  fregas Jan 7 '13 at 22:31
1  
Also, while it's not technically part of your answer, here's an interesting bit of trivia: you can use kill -HUP as root to force someone's terminal to hang up. You shouldn't do this without good reason. I get most of my mileage out of it when users leave shells running during maintenance and I need to unmount a filesystem that their shell is keeping open. If the user is connected but terminally idle, send the signal to their sshd process. Otherwise, if it's running inside of a terminal multiplexer, send it to the shell you want stopped. Only hang up the shell keeping you from working! –  Andrew B Jan 7 '13 at 23:36

No, any programs still attached to the terminal, and not placed into the background with something like nohup, would be killed.

This is why there are virtual terminal solutions like tmux and the older screen which create sessions which continue running even if you are disconnected, and to which you can reattach later.

share|improve this answer

As other's have mentioned, once you disconnect from ssh anything running within it is gone.

As @Michael Hampton and others have mentioned you can use tools like tmux or screen to disconnect/reconnect to terminals without loosing their content (i.e. child processes).

Additionally you can put a process into the background using an ampersand & and then use the command disown to disassociate them with the current shell.

# start a command
% sleep 5000 &
[1] 3820

# check it
% jobs
[1]+  Running                 sleep 5000 &

# disown everything
% disown -a

# check it again (gone from shell)
% jobs
%

# but it's still running on the system
% ps -eaf|grep "[s]leep"
saml      3820 23791  0 00:16 pts/1    00:00:00 sleep 5000
%
share|improve this answer
    
Is it possible to reattach a terminal session to a disowned process? –  Fake Name Jan 6 '13 at 7:25
3  
Yes. See this U&L question for details: unix.stackexchange.com/questions/4034/… –  slm Jan 6 '13 at 7:33

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.