Tips 3 - GNU Screen

GNU Screen has become a fundamental force in reshaping window management tools everywhere. Today, in most production environments you’ll be using something like TMux or something like it, but that’s overkill for this class.

Besides, this class is about old school hacking! So we’re going to teach old school Linux screen.

So what’s a window management tool?

Say you’re working hard on your crawler, doing magical things in C, crawling the web, whatever. All of a sudden, Dartmouth “Secure” cuts Wi-Fi (you know how things get). You curse and scream, but the gods refuse to hear your pleas. You’ve lost all your work. Maybe you save frequently, fine. But how often? At least you’ve lost maybe 30 minutes of work? What if this was the moment you finally had a break through and figured out how to get the code working?

Let’s take another scenario. Couple weeks from now, you’re working on the final finishing touches for your search engine, but it’s a total mess. You have one terminal for the crawler code, one for the indexer code, a terminal for the search engine code, another terminal to run the crawler, one more to run the indexer, and one final one to run the search engine… Wow.

A window management tool solves both these issues. Screen allows you to have separate “windows” (or tabs, if you prefer) within a single SSH connection. This cuts down the number of physical terminal windows you need significantly.

Ah, but where do you configure screen? By now you should have some intuition to how a linux environment works. You guessed it - in your ~/.screenrc.

A helpful starting point maybe what’s in my ~/.screenrc:

# skip the startup message
startup_message_off

# Automatically detach on hangup
autodetach on

# Change default scrollback value for new windows
defscrollback 10000

# start with visual bell as default
vbell on
vbell_msg "bell on %t (%n)"

# look and feel
caption always "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= @%H - %LD %d %LM - %c"

activity "Activity in %t(%n)"

Of course, you should play around with it until you get one that satisfies you.

Let’s get started using screen. Thankfully, both Mac OS X screen and standard linux screen are basically the same, so it doesn’t matter where you use it. Of course, practically speaking, you wouldn’t be using screen for local purposes, but this is just to get used to it.

Pull up your favorite terminal and simply type screen. It may not look like much has happened - you should still be in the same directory in which you called screen. On the bottom, however, you’ll see an indication of which tab you’re on (there should only be one right now). For me it looks something like this:

0 ../website/cs50                      @chanders-mbp - Sunday 26 January - 16:07

0 being the tab number, ../website/cs50 is actually the tabname, but it defaults to the directory I’m in, followed by my username and the date. So let’s run this super important program of ours.

while [ true ]; do echo "hi"; sleep 60; done

Great, now we have some infinite loop running. Hey, while this super important program is running, wouldn’t it be great if we could do some other stuff in the mean time? Sure, why not.

Fire up a new window with <CTRL>-a-c. All screen commands begin with <CTRL>-a. You can configure this of course, but personally I find the defaults to be pretty good.

Now you have a second tab in the same directory. The line should be the exact same, except now you have a 1 where the 0 was, indicating you’re on the first tab instead of tab zero. Cool! Now let’s start another program here.

while [ true ]; do echo "bye"; sleep 30; done

Aren’t those long tabnames annoying though? Let’s rename the tab - I don’t care what directory I’m in, and besides I can always find out with pwd. <CTRL>-a-A renames a tab. You can rename a tab even though it’s currently executing an infinite loop! Pretty nifty.

How do we switch back and forth between tabs? <CTRL>-a-n for the next tab and <CTRL>-a-p for the previous.

But what if you had lots of tabs? <CTRL>-a-# where # is a number between 0 and 9 will move you to that tab number. What if you have more than 9 tabs? There is a solution for that, too, but in general it’s best to not go crazy on these things.

If you truly have a lot of tabs, another solution (and a better one) is <CTRL>-a-". That’s a double quotation mark, for those whose browsers don’t support this font or have rendering issues. This will vertically list all the tabs, and selecting between them is standard Vim-style navigation. (Why do we still call it Vim-style? Who uses anything else? It should just be called navigation..)

Now let’s detach our running process to have it run even as we exit screen. <CTRL>-a-d detaches a process so it runs without you. Now when you close your laptop and go play pong, it will still be running - and you can come back and reattach. To see all the screen sessions that you were running, when you’ve exited screen type screen -ls. Even though we have multiple tabs opened, it only counts as a single screen session. To reattach, simply press screen -r in your terminal, and it brings you right back. If you had multiple screen sessions, it would show you the PIDs and ask you which one.

To kill one tab in particular, press <CTRL>-a-k. It will ask you if you really want to kill this window, and press y in response.

As a final note, there are some differences between linux screen and GNU screen - not all linux distros use standard GNU screen, and there may be some obscure key combinations that aren’t kept or maintained. But for the most part, they are basically equivalent.

Here are some useful guides. Do check out this one and its screen logging section - that’s really useful. And here is one about GNU screen specifically.

The Holy War

Wait, I thought the holy war was Vim vs Emacs? Well, there can’t be much of a war when Vim has won - it’s more of a pounding. So…

Also, we have lots of holy wars. We’re computer scientists, it’s what we do when we’re not reading Isaac Asimov.

Tmus vs Screen

Such Holy Wars

Wow

Much arguing

Kewl terminals

Sidenote, Mac users should try pip install doge (install python to use pip). It’s the most awesome project ever

Seriously though
Seriously though