How to Reattach to an Existing “screen” Session?

The “screen” is an external Linux utility that allows the user to open multiple terminals with a single terminal window manager. The screen utility can disconnect the different terminals temporarily, i.e., “detach” the screen and can connect them back as per requirements, i.e., “reattach” screen session.

While detaching, the processes keep on running when the screen session detaches and continues back at the same point where the session is disconnected.

This guide explains all possible methods to reattach an existing screen session.

Prerequisites: Install “screen” in Linux

The “screen” tool is generally pre-installed in the Linux operating system. However, if it is not, you can install it on your distribution using the commands: 

$ sudo yum install screen                     #For CentOS/RHEL
$ sudo pacman -S screen                       #For Arch based
$ sudo dnf install screen                     #For Fedora
$ sudo apt install screen                     #For Debian based

Method 1: Reattach to an Existing screen Session Using SID

Every detached screen session has a unique SID that is used for the identification purposes in case of multiple screen sessions. It is assigned automatically when the session is detached. Let’s see how it is useful to reattach the existing screen session.

Step 1: Detach Screen Session

The detach screen session means disconnecting the existing terminal temporarily and putting it into the background. The task performed in that session remains in progress.

To detach, press the key “CTRL+A+D” to send that process to the background: 

$ wget https://download.geofabrik.de/europe-latest.osm.pbf

Note: This is just a command. The users can try any other command and follow the process.

Step 2: List Existing screen Sessions

When the screen session is detached, then open the new terminal and run the “ls” command to check the list of existing screen sessions:

$ screen -ls

The output shows only one “detached” screen session having SID (screen session ID) “3280”.

Step 3: Reattach Existing screen Session

To resume/reattach the existing screen session use the “-r(reattach)” switch with the screen command and the screen session ID, i.e., “3280”:

$ screen -r 3280

The existing screen session has been reattached now and the downloading of the script has also been resumed.

Method 2: Reattach to an Existing screen Session Using Name

Apart from the screen session id the user can also detach and reattach the existing screen session using the creative names for identification purposes. These names are set manually as per user choice.

Step 1: Set a screen Session Name

A new screen session is created named “first-session” using the “-S” flag of the “screen” tool:

$ screen -S first-session

The session has been created. Press “Ctrl+A+D” to run it in the background, i.e., “detach”.

Step 2: List the screen Session

Execute the “ls” command to check the list of existing screen sessions:

$ screen -ls

The screen session named “first-session” has been detached.

Step 3: Reattach Existing Screen Session

Specify the existing screen session name “first-session” with the “-r(reattach)” switch of the screen command to reattach it:

$ screen -r first-screen

The “first-session” has been reattached successfully and there is no program running in it.

Conclusion

To reattach an existing screen session, use the “screen” tool followed by the “-r(reattach)” argument. An existing session can be reattached by specifying the session ID, i.e., “SID” or by the “specific name” with the “screen -r” command.

This guide has demonstrated all possible ways to reattach an existing screen session in Linux.