Screen tool. Managing multiple sessions and screens via one terminal.

screen-terminal-session-linux

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 Ctrl+a 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)

  • Ctrl+a c Create a new window (shell)
  • Ctrl+a " List all windows
  • Ctrl+a 0 Switch to window 0 (can be 0-9 )
  • Ctrl+a A Rename the current window
  • Ctrl+a S Split current region horizontally into two regions
  • Ctrl+a | Split current region vertically into two regions
  • Ctrl+a tab Switch the input focus to the next region
  • Ctrl+a Ctrl+a Toggle between the current and previous region
  • Ctrl+a Q Close all regions but the current one
  • Ctrl+a X Close the current region

Happy screening!

Bonus:

Extra functionality of SCREEN

Customizing 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

or

sudo vim ~/.screenrc

Logging Your Screen Output

A great function to log screen and trace activity.
Ctr+a H

Receiving alerts

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.
Ctrl+a M will monitor for an activity or an output in the screen
Ctrl+A _ will monitor for the inactivity of the screen (downloading big file)