SaltyCrane Blog — Notes on JavaScript and web development

How to start a long-running process in screen and detach from it

How to start a long-running process in screen, detach from it, and reattach to it later.

Start a long running process in screen and detach

Reattach to the existing screen session

  • Ssh to the remote host again:
    [email protected]:~$ ssh myremote 
  • List your active screen sessions:
    [email protected]:~$ screen -ls 
    There is a screen on:
    	11518.pts-1.myremote	(Detached)
    1 Socket in /var/run/screen/S-eliot.
  • Reattach to your screen session:
    [email protected]:~$ screen -RD 
    Note: you don't actually have to use the -RD option. You could use -rD or -r. But I just use -RD all the time. If there is more than one screen session active you will have to say: screen -RD 11518.pts-1.myremote or whichever screen session you want to attach to.
  • It will show you the "sleep 3600" command running. To exit, CTRL-C the sleep process, type "exit" to exit the screen session, and "exit" again to exit the SSH session.

See also


#1 jnns commented on :

screen was somehow deprecated if I remember correctly. tmux seems to be a more up to date alternative.

#2 Eliot commented on :

jnns: I guess I am behind the times. tmux looks really good-- I will have to give it a try.

#3 levi v commented on :

Instead of hitting : and typing "detach" you can also just do ctrl+a d. That will detach you instantly.

Also when reaching, checkout -X option, Then you can view/type in it from multiple terminals.

Also, if you like tmux, checkout byobu.