How to Detach the Screen Session on Linux

How to Detach the Screen Session on Linux

Learning how to detach the Screen session on Linux is vital for multitasking. We’re here to help you understand everything about it. 

In this article, we will discuss the Screen software in detail. Also known as GNU Screen, it is a widely used software for creating multiple sessions from a single secure shell (ssh) session. We will discuss the main steps for installation and usage in Linux. Finally, we will discuss the steps for detaching and reattaching to a screen session in Linux.

Introduction to Screen

Screen is a handy Linux utility that can create and use multiple sessions from a single secure shell (ssh) session. This is the multiplexing of terminals and a wide range of additional features. The following are the essential features of Screen software:

  • Multiplexing of multiple sessions over an ssh session
  • Despite network disruptions, it can establish and control access to network session
  • You can disconnect and reconnect to an existing shell session from any other computer
  • It can daemonize a session to support a long-running process. Later, one can attach to that session when required
  • You can send a running application to the background and connect to it.

Usage of Screen software

We will now discuss the use of the Screen software.

Check if the Screen software is installed on the computer

We will first check if the Screen software is installed on your computer. Simply click on the System Menu and select the Terminal to launch it. Now, type the following command on the Terminal:

$ screen –version

Installing the Screen software (if required)

The previous command should display the Screen software version. If the software is not installed, you can install it by running the following command on the Terminal:

$ sudo apt install screen

The above command can be used on both Ubuntu and Debian to install the Screen software. For other operating systems (OS) such as Cent and Fedora, run the following command:

$ sudo yum install screen

Starting Screen session

After the installation of Screen software, it can be started using the following command:

$screen

The above command will start the Screen session, a welcome message will be displayed, as shown in the snapshot below. 

welcome screen

After the software starts, use the space key and return keys to navigate the windows. You can also use the Ctl+a to list down help. This will display the list of commands available for Screen software.

Starting a session and specifying its name

In the previous section, we created a session without specifying any name. You can also create a session specifying its name (such as writers) with the following command:

$screen –S writers

How to Detach the Screen Session on Linux and Reattach it

After running the screen command for the first time, the created session will become the first session. We will now discuss the commands for detaching and reattaching to screen sessions.

Listing down the active sessions

You can list the active sessions by running the following command:

$ screen –ls

The above command will list down the sessions as shown below:

sessions list

Also, you will see that you are attached to the current session. 

Detaching the session

You can send a session to the background, i.e., detach a session. This can be performed by using the keys Ctrl + A and then D. This will not only send the session to the background but will also display the message that the current session has been detached. This message includes the session’s identifier (ID) and the session’s name (if specified earlier).

Now, if we list the session again (using the command discussed in the previous section), you will see that the session will be marked as detached. This means that the session is running in the background. So, if you are running the session from a remote computer and your internet is not stable and your connection breaks, you can still connect from the same or another computer. The process that is running in the shell will remain active.

Reattaching to a session

You can reattach to a session by using the –r flag along with the session Id or its name. The following command shows how to reattach to a session:

$ screen -r <session Id>

After running the above command, the corresponding session will be reattached and open the process running in the background. You can also specify the session’s name instead of the ID and attach it to it using the following command:

$ screen -r <session name>

Closing a session in Screen

You may be running a command in the session. So, when you reattach the session, you will see the process running. Once the process is completed, you can close the session by using the following command:

$exit

After running the above command, the current session will be terminated, and you will see a message on the other active shell that the session is terminated. Had it been the last session and you ran the ls command to see the list of sessions, it would display none.

In this article, we have discussed Screen software and its features. We then looked at the Screen command’s installation and usage steps. Finally, we discussed how to attach and detach a session based on Screen ID and name.

If this guide helped you, please share it.

Leave a Reply
Related Posts