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

If I was running a command before the SSH connection was dropped, will the command continue executing?

share|improve this question

5 Answers 5

up vote 6 down vote accepted

In most cases, no. Processes will be sent a SIGHUP on loss of terminal. You can prefix a command with 'nohup' to ignore the signal. See:

http://en.wikipedia.org/wiki/Nohup

share|improve this answer

Not generally, although not knowing what shell or even what OS you're running, it's tough to say. If you were running it under screen, for example, I think the behavior is to detach and continue running. Maybe some other shells have that as a configurable option.

share|improve this answer
1  
+1 for mentioning screen. manpagez.com/man/1/screen –  jscott Feb 23 '10 at 20:46

As Warner says above, the child of the ssh daemon (that is the login shell and it's children) will get SIGHUP, but they won't get them instantly. There will be a delay, sometimes in minutes before the ssh daemon on the server side gives up on your connection. During that time the process will continue to run.

As Warner also said, the process can choose to ignore SIGHUP, in which case, it will continue to run until it has to request input and then find STDIN has closed.

share|improve this answer

I'll add another heplful suggestion to the thread, something I just learned few minutes ago:

If you already started a process that will take a long time (tar restore in my case), and forgot to include nohup in front of it, you can still prevent it from terminating on logoff.

Here are the steps:

  1. Press Ctrl + Z - this will suspend the job
  2. Enter disown -h %x, where x is the job number you get after suspending the job
  3. Execute bg to put the job in the background
  4. Exit the shell and go on with your life :).
share|improve this answer
    
+1, however I have found that the disown command is not necessary under bash on linux. I only need to bg the job, then log out, and the job continues running. –  njahnke Jul 27 '12 at 16:48

Warner's answer is spot on. Alternately, you could also execute the command in the background, by appending an ampersand (&) to the end of the command.

share|improve this answer

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.