GNU screen attach

The scenario where I use screen the most, is when I need to run a long process in a remote server, like compiling something on Gentoo, downloading a lot of files, rsyncing, etc.

I can open a ssh session and run the command, but in that case I need to leave my local computer on, and there is the risk that for some reason the session could terminate and in that case the running application will also be terminated.

So, what I do in those scenarios is, to open a screen session, and run the application in there, then I just turn my local PC off, and I can come back some hours later to check how is everything.

First install and configure screen for the first time.

How to attach to an existing screen session.

There are some options, let’s see two, and the best one (at least for me)

screen -r

Attach the running session of screen

screen -r [name]

Attach a specific screen session, you need to use screen -ls to list the running screen’s sessions.

screen -D -R

This is a great one, if you are attaching again, after some period of time, specially if you are now on a different pc, this, will detach first the screen session if necessary, and even logout remotely to attach the session for you.

Now if you combine this powerful command with ssh -t, you do not even need to open a ssh session first.

ssh -t user@remote.server screen -D -R

This is my favorite, once you finished, you do not even need to bother detaching or closing, just turn the local pc off, or close the terminal window.

From the screen homepage:

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ANSI X3.64 (ISO 6429) and ISO 2022 standards (e.g., insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.