Do you want to run multiple virtual screen sessions from a single terminal console? Or avoid Internet connection drops from terminating your console session via SSH? If so, welcome the GNU Screen terminal multiplexer into your toolset today!
Using The GNU Screen Utility
The GNU Screen utility for Linux is a versatile screen multiplexer which allows you to start multiple virtual screen sessions (think about them like additional console windows) from within your current terminal session!
Getting started is easy. You can install the GNU Screen tool using
sudo apt install screen at your terminal command line:
If you are using a RedHat/RPM based operating system, please substitute `apt` to `yum` in the command above.
To open a new virtual console, you can now simply type
screen and hit enter, which will present you with a splash screen:
Simply press space or return to close the splash screen. The command prompt will seem to return, but in fact you are now within a virtual screen session. Think about it like an alternative terminal session, running inside the former terminal session.
One of the great things about GNU screen is that even if the main/parent terminal session where to somehow fail or crash (for example due to a lost network connection), and even when you close or terminate the main/parent terminal session, this screen session will remain live and running!
It is easy to check if we are currently inside a screen session or not:
This if statement tests if the
$STY variable is set or not. If the
$STY environment variable is set, then this is a screen session.
So how do we go back to our main/parent terminal session? This can be done by using a keyboard combination which GNU screen will be listening for in the background. Simply press CTRL+A > CTRL+D. It does not matter if you release or not release the CTRL key while doing so.
You will see something similar to this output:
This also tells us we have only detached from the virtual GNU screen session – it has not terminated, and if we want we can connect back to it. Even if for some reason the main/parent session had closed, the virtual screen session would not have terminated and we can again connect back to it.
To reconnect to the session, we can issue a simple command:
Confirming this command will simply return us to the virtual screen session we had running. Notice that we used the relevant process ID (as reported when we detached from the screen session) to reconnect with the virtual screen session. This also immediately clarifies how we can start an indefinite amount of screen sessions (though each one will consume some amount of resources) and connect to whichever one we like by using the
screen -d -r command.
We can read more about the options we used for this command in the screen inline manual:
For a wider overview of screen and it’s many functionalities, you can use the
man screen command:
If we want to see a list of screen sessions which are running on the machine at this moment, we can issue a
screen -list command:
Note how the session reports as ‘Detached’ or ‘Attached’ depending on whether you are still connected with it somewhere or not.
Sometimes you may like to scroll back inside a screen session, for example when more lines of output were generated then the amount of lines which is available in your terminal window size. One would expect this to be as easy as using the mouse to scroll up and down, but is not.
Instead, you can use a CTRL+ESC keyboard combination to enable copy mode (though no actual copy is taken if you only do scrolling), and now your mouse, as well as the standard up/down cursor keys will work correctly. When you’re done with looking (and/or copying if you like, for example with your mouse selecting and copying as normal), simply press the ESC key again to abort the copy mode.
At times a program running inside a screen may crash, for example due to a program error or out of memory or disk situation. If this happens, the screen will show as ‘Dead’ in the
screen -list output:
At such point, we can wipe away the dead screen session by issuing a
screen -wipe command:
In this article, we explored how to install the GNU screen utility, how to start a new virtual screen session using the same, and how to detach from and reattach to an existing screen session. Finally we look at how to scroll inside screen sessions, as well as looking at how to handle dead screen sessions. Enjoy!