How to use Linux SCREEN terminal multiplexer tool?
screen
is a terminal multiplexer tool. Put simply, it means that using screen
we can run multiple terminal sessions within a single window/ssh
session. When we start a process using screen
, it can be detached, and the process will not be terminated. It can also be reattached later without affecting the process in any way.
The first thing to do is to check if your system comes with screen
pre-installed, and the easiest way to check that is to run the following command.
screen -v
If there is an output of the form, Screen version 4.06.02 (GNU) 23-Oct-17
, that means you have screen
installed.
If not, we install it using package manager
.
Installing screen
tool in Linux
To install screen
on Ubuntu, we can use apt
, to search and query for screen
and then install it, with the following command.
sudo apt install screen
Use screen
command in Linux
To use screen
, we just type:
screen
This starts an unnamed session, and the terminal looks the same as it would without screen
. To check whether screen
has actually started just press the following key combination Ctrl+A+?
, and if screen
is running, the keybindings used by screen
are presented to the user.
Above, we can see the default keybindings used by screen
, and that the command key is ^A
, where ^
is the Ctrl
key. Hence to run any command, we first press the command key, followed by any key that we want.
Named sessions in screen
command
Say we want to organize our sessions, and remember where we are working on what. For example, using the terminal, we are playing music, and showing album art at the same time, and we want to keep this session independent of our programming environment. screen
lets us create named sessions, with the following syntax.
screen -s sessionName
This way we can give meaningful names to the sessions, and keep them organized.
Listing sessions - screen
command
To list our running sessions, we run:
screen -ls
Hence, if we created a session with the name music, on listing the sessions, we should get something like
We could also use the command windowlist, which is run by the following key combination ^A+"
.
This gives us an output like the above, which is a selectable list of windows, with sessions in them.
Renaming and starting new sessions - screen
command
To rename the window, we use the combo ^A+A
. And to start a new session, from within a currently running one, we use ^A+c
.
Say we start a new session, and then rename it to math, using the previous key combos, we get the following when we open the selectable windowlist.
Killing a session
To kill a session, we have various methods available, depending on whether we are within a session, or outside one.
When outside a session, either the PID or the session name should be available to the user executing the command to kill the session. The syntax for the same is:
screen -X -S PID quit # quit is the command to end a session, and kill all windows.
Hence for the list of running sessions is as follows
We can specify the PID to the point where it can be distinguished as unique. Hence, only 5 is enough to terminate the session. The same thing can be done for contracting names.
When inside a session, to kill the session, we use ^A+\
. But to kill only a single window in the session we use ^A+k
or ^A+K
.
screen
as a multiplexer
screen
can split one window, both horizontally and vertically, and we can then accordingly start a new window in that split, or open a secondary window. To split a window vertically, we follow the command key with |
(vertical pipe). Hence, on splitting vertically, we get something like the following.
To cycle between splits, we use ^A+Tab
. Using the selectable list, we can either open an already existing window, or we can create a new one, with the key combo ^A+c
. On opening the current window in the vertical split, our terminal finally looks like below.
To split the window horizontally, we use ^A+S
. And to close a pane, we use ^A+X
(remove).
Detach and reattach screen session
The most powerful and common use of screen
is to detach and reattach screen
sessions. For example, we want to run a download, and not have it in the background, since it would occupy the terminal and make it unusable to use screen
. We can start a screen
session and then detach it. The process keeps running in the background as a screen
process without interfering even once.
To detach a session, the key combo is ^A+d
. On detaching we get something like the following, and are returned to our original terminal.
To reattach a session, the command has the following syntax
screen -r -S <PID/sessionName>
Any panes, and/or splits are lost on detaching, but all windows are still present with whatever processes they were executing.
Conclusion
screen
is a powerful tool, letting us run multiple terminals simultaneously in a single session, and let us process multiple tasks parallely, and even in the background, without letting it interfere with whatever else is being done. This tutorial covered how to use the tool screen
to its full capabilities.