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.

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?

share|improve this question

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.

3 Answers 3

up vote 5 down vote accepted

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

share|improve this answer
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 
    
share|improve this answer

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.

share|improve this answer

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