Thursday, August 27, 2015

GNU Screen Awesomeness

Introduction

I have been using GNU Screen for a LONG time; it usually comes by default with every *nix distribution that I have used. It is one of my favorite tools in my *nix toolbox. Basically it allows you to multiplex your SSH session allowing the ability to switch between full screen shell windows. I recently worked on a project which used Linux and utilized the tool pretty heavily. I thought everyone knew about Screen, except my fellow developer had never heard of it, so I figured this topic would be great to share.

Basically, I can have screens for GNU Midnight Commander, MySQL REPL (sometimes I create a tunnel and use Workbench, but I don't always have access to it), my source directory, and a configuration directory. This allows me to quickly switch between different tasks/locations without having to do a lot of heavy lifting or have multiple SSH connections or change directories a lot.

This is all well and great, but it comes with another great benefit. If you are disconnected for some reason, screen will keep everything waiting for your return. You can also suspend your screen session via a keyboard shortcut and resume it just the same.

Let's get started.

Getting Started With Screen

It is really easy to start screen:
$ screen
You are in!
NOTE: The keyboard shortcuts all require the Control key, it is usually abbreviated with Ctrl, but the below will use "C-" because that is how Screen's help specifies.
Now you have a LOT of shortcuts, but below are the most basic and useful:

Create new window
C-a, c
View Windows
C-a, w
Switch to Window
C-a, # (index of the window)
Disconnecting/Detatching
C-a, d

Now, A Bit More Advanced

Name Your Session
C-a :sessionname bars
Let's test this out, create a named session.
screen -S sessionname
Enter some commands at the new prompt. And then press
C-a, d
Now you should be back where you were before you started Screen. Even if you were you were running Midnight Commander (or anything else). Now let's jump back into the session.
screen -r sessionname
That is it. So easy. Use it!

No comments: