Skip to content

Using "screen"

Andrea Telatin edited this page Oct 24, 2023 · 6 revisions

Using "screen" to save your remote sessions

Why?

A typical problem when connecting to a remote machine is that if the connection drops, the remote server will believe we are no longer interested in it and will kill our session. If we were performing some analysis, it would be terminated and we'd have to run it again.

Sometimes the analysis is long, and even if we have a stable connection we need to keep our computer on just to let the remote server know we don't want to close the session. There is a handy tool to make our life easier: screen.

With screen we can create a terminal that will not disappear when we disconnect from a machine, and that will wait for us to come back when reconnecting to the server.

How?

In this section we'll see how to

  1. create a new working session,
  2. how to connect to an existing session and
  3. some tricks to use screen as a pro.

To create a screen session

  • Log in to the remote machine.

  • ⚠️ First of all, we have to check no other screen session is active: screen -list

  • If the previous command gives no output (or a message like No Sockets found in …), then we can create a new screen, otherwise we need to attach to the previous session:

    • To create a new screen session, type this command: screen -S course. With the -S switch we gave our session a handy nickname (“course”), but it's not mandatory to do so, but can be very helpful.
  • Now we are inside our "screen" and we'll have to press ENTER to get rid of a welcome text. If we close the terminal window, we don't lose what we were doing.

To reconnect to an existing screen session

  • To re-attach to our previous screen: screen -dr course

Interacting with the screen program

Since the screen program shows us a Linux terminal, when we type we are talking to the terminal itself. So to interact with screen we need a special shortcut: Ctrl + A. This triggers screen to listen to us. Then we can type a second key to specify what we needed:

  • Ctrl + a, c to create a new terminal inside our session
  • Ctrl + a, p to navigate to the previous terminal
  • Ctrl + a, n to navigate to the next terminal
  • Ctrl + a, d to detach from the session: we'll return back to the “parent” terminal where we can reattach to the screen session with screen -r course.

Further reading

For a more information you can read this article about screen.