The screen command makes it possible to send the running applications to the background, and you can reattach them when necessary. The command acts as a terminal multiplexer. You can utilize its various options to create a new shell window that you can send (detach) or pull from the background (reattach) to resume using it.
With the screen command, you can reattach your process which is running in the background even if your session accidentally disconnects, such as when accessing a remote machine. That way, your work won’t get interrupted or lost in case of a long process.
This guide covers everything about using the screen command, from installation to detaching and reattaching the screen session.
How to Use the Screen Command
The GNU screen allows creating a screen session and sending it to run in the background, such that even when your connection breaks, you can pull the process from the background and your process will still be active.
You can create multiple screen sessions and navigate each using various options. First, check if you have the screen installed using the following command:
If not the screen is not yet installed, here’s how to install it on Ubuntu and Debian:
Use yum for CentOS and Fedora:
You can start a screen session using this command:
A welcome page will appear like in the following image. You can press the space or return keys to navigate the window.
To quickly get started with the various commands, open the list of commands for screen by pressing the Ctrl+a ? keyboard keys. You will get all the commands displayed as follows:
Detaching and Reattaching the Screen Sessions
Once you run the screen command, the active shell becomes the first screen session. You can confirm the active sessions using the following command:
We only have one attached session which is the current shell. On the right, you will note the attached message to imply that it is the active shell.
We can send it to the background(detach) by pressing the Ctrl+a d keyboard keys. You will get a message like the one in the following illustration which confirms that the screen session has been detached.
The message displays the screen session ID. If you defined the name for the session, it is also displayed in the screen session.
If we list the available screen sessions, the session is now detached. This means that it’s running in the background and the process on the shell is active. That way, if the connection breaks, like in the case of accessing a remote server, we can access the session. Our process that runs in the shell will still be active.
To reattach the screen session, use the -r flag followed by the session ID or its name. In this case, we have its session ID as 5608. We run the following command:
Once you execute the command, the session will reattach and open the process which runns in the background like in the following case where the background process runs an upgrade and update command. You will find your process still running or completed.
If the process is completed, you can close the screen session by typing exit on the terminal:
You will get the message that the screen is terminated on the other active shell. If that is the only screen session, the listing of the active sessions will display none.
Naming a Screen Session
So far, we detached and reattached a screen session using its ID. However, if you have multiple screen sessions, you can create a custom name for each for easy identification using the -S option.
Let’s create a screen session named linuxhint-screen. Use the following command:
If we list the active screen sessions, we have our session created successfully.
You can detach the named screen session by pressing the Ctrl+a d keyboard keys. You will get the detach message which shows the session ID and its name.
To reattach it, use its name instead of the session ID as shown in the following:
Also, typing exit closes the screen session. You will get a message which confirms that the session is terminated.
Conclusion
The screen command lets the users detach and reattach the screen sessions, such that if the connection breaks, their session still runs in the background, uninterrupted. You can pull it anytime. To detach a screen session, use the ctrl+a d command. This guide covered all the details about screen sessions in Linux including the steps on how to install it, create a session, detach it, reattach it, and name the session.