When working on the terminal either locally or via ssh, many times it is needed to open an additional terminal while a process is already running on the first one. For example, you are downloading a huge file and it needs 40mins or more to finish and you want to do something else on a new terminal while you are downloading the file. There is a really handy tool in Linux called “screen” that can help overcome the limitation especially if your working through ssh and don’t want to open multiple ssh sessions. You can have multiple screens, each one of them running independently and you can switch between them using keyboard shortcuts!
Try running on your terminal
screen --version to check if it is already installed.
If not you can
sudo apt install screen or
sudo yum install screen
After you install it you can run
screen in order to start the tool. Once you run screen you will create a new session and a blank screen will open. You can have a session with multiple screens and you can have also multiple sessions. A session can be detached and reattached when needed. You can have a screen session running even if you disconnect from your ssh session and reattach to it later on.
If you run
screen -S mysession you will create a session named “mysession” (which makes it easier to recognize it later on when needed. You can detach from a session with
d. You can type
screen -ls in order to list all the sessions you have(you can see the session id here). You can reattach to a session by typing
screen -r followed by the session id or session name.
A session is killed once all the screens inside that session are closed.
Once you create a session you can try the following commands-shortcuts:
(first, type CTRL+a and then release and type the next character)
cCreate a new window (shell)
"List all windows
0Switch to window 0 (can be 0-9 )
ARename the current window
SSplit current region horizontally into two regions
|Split current region vertically into two regions
tabSwitch the input focus to the next region
Ctrl+aToggle between the current and previous region
QClose all regions but the current one
XClose the current region
Extra functionality of SCREEN
Like other linux applications you can edit the configuration of the screen by editing either the users config file or the system-wide config file:
sudo vim /etc/screenrc
sudo vim ~/.screenrc
Logging Your Screen Output
A great function to log screen and trace activity.
Amazing functionality to get alert when there is a change in one of the screens. Very useful if for example you are downloading a big file and you move on to another screen and want to get an alert when the file while finish downloading.
M will monitor for an activity or an output in the screen
_ will monitor for the inactivity of the screen (downloading big file)