Linux ‘screen’ command

‘screen’ is a really useful linux command what allows you to “window manage” a text console.

Why would you want to do that?

You want to edit a (config) file, restart the service, and view the output logs at the same time. Ok, imagine no GUI to start with. No “Gnome Terminal” tabs / windows nicely arranged.  You could switch (Ctrl-Alt-Fn) between the various virtual consoles – but that’s a faff if you want to see things happening in near-real-time.

You can use ‘screen’ instead to manage a console for you – window-ising it, and run different processes / commands / vim / logs in each “window”.

It’s not installed as default on all flavours, but usually forms part of the base repos, so fairly easy to install:

RedHat / CentOS:

sudo yum -y install screen

Ubuntu / Debian:

sudo apt install screen

Starting it is easy:

me@lmachine:~$ screen

When using ‘screen’, I find that it’s best to think of a ‘region’ as a “window” or “compartment” of the view (yes, technically ‘window’ does mean something slightly different in ‘screen’ terms).

Then you are greeted by the ‘welcome screen’ (pun intended) and need to know a new commands, most of which are initiated by pressing Ctrl-A. Here ae a few of the ones I’ve learned to use / I think are most useful:

Ctrl-A | – Split the current ‘region’ into two ‘regions’ vertically
Ctrl-A S – Split the current ‘region’ into two ‘regions’ horizontally
Ctrl-A <TAB> – Switch between ‘regions’
Ctrl-A X – Remove the current ‘region’
Ctrl-A Q – Kill off all ‘regions’ but the current one
Ctrl-A k – Kill off the current window (doesn’t kill ‘regions’)
Ctrl-A c – Create a new window with a shell and switch to that window (can be done inside a newly created ‘region’
Ctrl-A A – Set the title of the window

Below is an example, where regions have been used. First the screen has been split into vertical regions, then within the second region, that has been further split into horizontal regions. Within each a new ‘window’ has been created with a shell. You can then run something different in each (left: a log, top right: mysql client, bottom right: bash prompt).

screen_windows_size

[Image linked from: http://www.geekpills.com/operating-system/linux/linux-screen-command-examples%5D

The GNU Screen Manual is available here: https://www.gnu.org/software/screen/manual/screen.html

Advertisements