This question already has an answer here:

I have a remote Linux server and I use it to run some very long tasks using SSH. It works great, but, of course, if the connection dies for some reason, the task dies.

Specifically, I'm running something like this:

[myName@localStation]$ ssh john_doe@myRemoteServer
Password: *****
[john_doe@remoteServer]$ ./myVeryLongTask.script > myOutputLog.txt

Is there a way to tweak the SSH connection in such a way that, if the network connection fails, the task keeps running?

marked as duplicate by jasonwryan, Anthon, slm, rahmu, Mat Sep 13 '13 at 8:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You need to read up on the screen command (here's a quick google result)

Screen allows you to leave a remote connection running and come back to it for reasons exactly as you describe. It's also useful for running unattended jobs or keeping sessions open indefinitely.

'man screen' for more info

EDIT: Here's a better link to a HOWTO: screen: Keep Your Processes Running Despite A Dropped Connection

  • 1
    +1 And a month or so back I didn't know tmux existed, but after playing with it for a little, seems a bit more usable than the venerable screen. – msw Sep 13 '13 at 2:15

screen is indeed great for that. Another couple of options are at and nohup:

  • at

    at, - queue, examine or delete jobs for later execution

    You can either use at's interactive shell to launch the commands or save them in a text file and pass that to at:

    $ cat foo
    ./myVeryLongTask.script > myOutputLog.txt 
    $ at now < foo
  • nohup

    nohup - run a command immune to hangups, with output to a non-tty

    To launch a command do

    $nohup ./myVeryLongTask.script > myOutputLog.txt 

For the sake of completeness, dtach is another option. Summary of detach in the above link:

dtach is a tiny program that emulates the detach feature of screen, allowing you to run a program in an environment that is protected from the controlling terminal and attach to it later.

It's smaller in size compared to screen, and has much more functionalities than at or nohup.

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