Screen: Multiple Terminal Emulations in the same Terminal Window

Screen is a full-screen window manager that multiplexes a physical terminal between several processes. There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.

When screen is called, it creates a single window with a shell in it and then gets out of your way so that you can use the program as you normally would.

Start

In order to start a new session, you need to use the -S flag with a session name. This session name doesn't have to be unique but is recommended to reattach the session later on.

This will refresh your terminal with a new session that acts like a new terminal window.

$ screen -S "session-name"

Detach

At any moment, you may detach the session to return to your original terminal session. You may had a task running in the background in the screen session. This task will continue to opperate in the background without any limitation.

To detach press Ctrl + a followed by d

Ctrl + A , D

List

You may forgot the session name or you name two sessions the same way and can't re-attach to any of them.

Screen allows you to use the flag -ls or -list to display all the screen sessions name.

$ screen -ls

or

$ screen -list

It will produce something like this.

   18571.rtorrent  (Detached)
    1523.job       (Detached)
    1299.job       (Detached)

Attach

Let's say you want to take back control of the rtorrent screen session. You simply need to use the -R flag and the name rtorrent

$ screen -R "session-name"

This happend to me a couple time, I launched 2 sessions with the same name and both have a process running in the background. Since I don't want to stop the job too early I need a way to attach to those sessions. Turns out, you only need to specify the pid followed by . and the session name

$ screen -R 1299.job

After a connection lost

Imagine you are working from a different machine with am SSH connection and your ISP decides that it needs to refresh your connection. Working in a session will protect you from loosing anything. Although, you won't be able to reconnect because your session is still "attached" to your previous connection.

To detach your previous session, you need to use the -D flag. Now you can use the -R flag to re-attach like in the previous section.

screen -ls
screen -D "session-name"
screen -R "session-name"

Terminate the Session

When you are done with your session and want to terminate it. Simply type exit (when the session is attached) and you will be redirected to your original terminal session.

$ exit

Sources

  1. Using screen, why can't I re-attach to my session after a lost connection?
  2. How to detach a screen on a terminal