GNU Screen Tip #1

GNU Screen Tip #1

The emotion for this post is diligence.

I have been using GNU Screen for $idontknowhow many years. For those that are not aware, Screen is a program that provides what can be considered virtual terminals. Here’s a screenshot to give you some idea:

What even is going on here?!

Trey: “Okay Basil, that’s vim editing some file…”

Ah yes. That’s exactly what I wanted to show you….*rolls eyes*.

Now, let me type, in that terminal window, CTRL-A and then “2”.

What strange magic is this?!

Tred: “…I need an adult…”

No no, you’re in no danger. You’re perfectly safe with me. Truuuuust me…</joe-isuzu>.

But seriously, what just happened? GNU Screen is a class of full-screen window managers that can multiple terminals across multiple processes.

Tray: “……”

It lets you run lots of programs in one terminal window and switch between them quickly without having to actually open one terminal window for each program you want to run.

Tracy: “So, like, I can run Candy Crush, Edge, and TikTok all in one place?”


How did you even get here? Where’d Trey go?!

One of the neat tools of Screen is that you can run it on a remote system, and if for whatever reason your connection is terminated, whatever was running in your Screen sessions will remain running and you can resume your session whenever you next reconnect. I use for, among other things, keeping irssi running on one of my servers so I stay connected in IRC chatrooms on FreeNode, maintaining my state while in the process of server administration, or otherwise just having multiple sessions in various frequently used locations/paths on my server ready to go.

I also commonly startup two screen sessions on my workstation – one for my superuser, and one for my regular user. This lets me have quick access to capabilities as root and also see statuses on my system quickly, such as the myriad *top programs I am addicted.

This gets us to the point I wanted to make at the beginning, which is that I usually start the same 6 programs when I first startup my root session. Like most things I do, I got to thinking, “a computer can do this better”. And I looked-up how to startup a Screen session with your programs already running. Let me explain what I want:

  1. The first window will run dmesg --follow so I can see all my system messages and quickly find out if something I did broke in a way more serious way than I first realized…
  2. Next, I want to run dnf upgrade --refresh so I ensure my system is up to do. I run this command more than once a day usually because I am fastidious about ensuring I have the most up-to-date packages installed on my system. But I want this window to remain open even after it completes so I can run it again if I want to.
  3. Then, I want to htop running because I like to see how much of my investment into multicore systems has been wasted.
  4. Then I want to see iotop running because I want to see what’s lagging my drives and I want to be reminded again of how much I’ve wasted my investment into multicore systems.
  5. Then I want to see iftop running because I want to see what’s hogging all my bandwidth, who’s spying on me, and also make about 100% full use of at least *one* of the cores on my system by the end of the day (memory leak or what, I’m not sure).
  6. Then I want to see journalctl --follow _SYSTEMD_UNIT=unbound.service running.

Trudy: “Say whaaaa?! What was that last one all about?! You can’t just leave it like that!”

Oh yeah! Sorry, I run unbound locally to resolve my own DNS because I find that is usually a lot faster than relying on whatever the ISP provides. That will effectively tail the log and show me the hostnames I’ve resolved, and also neat things like cache hits and other things.

So, in its entirety, here’s my .screenrc, and I’ll point out the relevant section:

[root@Y700-15ACZ-BMG ~]# cat .screenrc
# Turn welcome message off
startup_message off

# Keep lots of scrollback
defscrollback 100000

# Use 256 colors
term xterm-256color

# Informative statusbar
hardstatus off
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'

# Turn welcome message off
startup_message off

# Use X scrolling mechanism
termcapinfo xterm* ti@:te@

# Fix for residual editor text
altscreen on

# Fix for Name column in windowlist only show "bash"
windowlist string "%4n %h%=%f"

# Start screen with these windows
screen -t dmesg time dmesg --follow
screen -t dnf bash -c "time dnf upgrade --refresh; exec bash"
screen -t htop htop
screen -t iotop iotop
screen -t iftop iftop
screen -t unbound-log journalctl --follow _SYSTEMD_UNIT=unbound.service

The section that actually achieves the wizardry I mentioned above is the portion after Start screen with these windows. Each line there specifies the title (the text immediately after -t and then the command I want to run. The two odd men out are the dnf and the last one. For the dnf session, I actually launch a bash process to run the dnf upgrade command followed by executing another bash session because otherwise it would close that session window once execution concluded. Thanks to the folks that posted at Superuser and Stack Exchange for the tips that helped me get this working.

Now, if you’re not at crazy as I am Screen can still offer a lot of value. Replace all of my silly, compulsive behaviors with your own and pick the programs you’d like to have rapid, always-on access to, or just come-up with your own uses.

I hope you at least found this interesting, entertaining, enlightening, or otherwise worth your time. Please feel free to share your thoughts, whatever they might be! I just was happy to share this.



Leave a Reply

Your email address will not be published. Required fields are marked *