I wrote this Gnu Screen guide in December ’08 with the previous incarnation of linux.com in mind. January 2009 however, linux.com started on its new and exciting path as a community site and the article just sat on my machine, waiting. So I decided to publish it on my blog in a four part series over the following month. Maybe someone can use it 😉
Have you ever had to run a time consuming process (say mkfs or compiling something) over a shoddy ssh connection? Or cleanly run several commands in parallel without having several open connections to that server? Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open? How about that ssh tunnel you need open and dont want to close by accident? And did you ever need to perform the same task on several different machines? Or connect to a serial terminal? Or maybe you just want a log file of what you re doing..
These are day to day situations any linux user will run into, that can be quite frustrating. That is, before you discover Gnu screen!
“Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.” Or at least, thats what the site calls it. Sounds easy enough, but there are an amazing amount of different uses for this incredible piece of software. The above list is some of the uses I could readily think about – some of the ways I regularly use screen, but like any decent linux tool – the sky really is the limit! I’ll be looking into different issues, so lets jump into some of the practical solutions for those problems.
The Fun Stuff – Nethack
Because linux is all about FUN (or at least written by fun loving people), screen has a cool option where all messages are given nethack style, instead of the standard bland wording. Screen has to be compiled using the Nethack option, but most linux distributions do.
The nethack option it toggled by the presence of the $NETHACKOPTIONS environment variable or a ~/.nethackrc file.
No other window.
You cannot escape from window 0!
Creating logfile "screenlog.0".
You add to your scroll of logging named "screenlog.0".
The nethack ones are actually More Fun, really!
The Fun Stuff – Different screen windows
Up till now, we covered using screen and the posibilities of several sessions. But because switching between sessions is not really what you d call user friendly or quick, screen has an interesting mechanism called windows. One screen session can contain several windows and switching between these is simple using a number of shortcuts. The existing screen terminal is always the first window. To create a new window, use the following shortcut.
C-a c C-a C-c (screen) Create a new window with a shell and switch to that window.
This creates a new window & switches to that window. Now you have two (or more if you want) different windows in the same screen session. To switch between the different sessions, you can use the following shortcuts.
C-a space C-a n C-a C-n (next) Switch to the next window. C-a backspace C-a h C-a p C-a C-p (prev) Switch to the previous window.
This is a good way to switch between the windows, but it quickly grows old when you have a lot of open windows to switch between. To make things a bit more manageable; screen has several other shortcuts.
C-a â€™ (select) Prompt for a window name or number to switch to.C-a 0 (select 0) ... ... C-a 9 (select 9) C-a - (select -) Switch to window number 0 - 9, or to the blank window.
These help, but there is still a better way.
C-a " (windowlist -b) Present a list of all windows for selection.
This shortcut gives the user an interface where he can select an open window.
Num Name Flags Â 0 bash $ Â 1 bash $ 2 bash $
And to keep this heap of windows organised, there are yet 2 more shortcuts.
C-a A (title) Allow the user to enter a name for the current window. C-a N (number) Show the number (and title) of the current window.
These are a lot of different shortcuts and mechanisms, but in the end – its all worth the effort! Lets take a look at the list of examples we made at the start of the article and solve another one!
- Or maybe you were programming and wanted a mysql prompt, a bash prompt and several text editors open?
I dont think the practical side of this example needs much more explaining. Opening the screen session, creating the different windows, renaming the windows and using them appropriately should be straightforward enough. Setting the whole thing up only needs to be done once, since you can simply detach the session, However if you need this setup on a regular basis, you can configure your “.screenrc” to load it at startup. More on the .screenrc later.
The Fun Stuff – Sending commands to all screen windows
Once you re used to the multiple windows, you might run into a situation where you want to send a same command to several of these open windows. Screen provides in the “at” command to do this. First you ll need to open command line mode.
C-a : (colon) Enter command line mode.
This way you can type a command once, but you ll still have to enter each separate window. But there is a better way. As an example we ‘ll send “ls -l” to all the windows.
at "#" stuff "ls -l^M"
This command is barely readable, so let’s pick it apart! The first part is ‘at [identifier][#|*|%] command’. The at command sends the text parameter to all the windows you specified in the identifier. You can match the criteria to either the window name or number with #, the user name with * or the displays, using %. The next part is the command you want to run in the selected windows. We’re using “stuff” to stuff the command we want to execute into the input buffer of the selected windows. Stuff is really straightforward. It simply stuffs the string you gave as a parameter. Next problem is the command. Or rather having it executed! To get screen to put an “enter” after the command, to execute the command, add “^M” at the end. You can do a lot more with this than just sending an ls to the input. Any screen command, like renaming, moving windows around, whatnot .. is available in combination with “at”.
This also applies to screen session using the -X parameter on the command line, for those special occasions where you just dont want to connect to the session or when you’re scripting something wholly exciting!
Special thanks go out to ï»¿Guy Van Sanden, who helped me figure out this command.
- And did you ever need to perform the same task on several different machines?
The possibilities of this command are vast and this is only one of the many that come to mind. The first and most obvious is when you need to close a screen session with too much windows to close by hand. Stuffing an “exit^M” into the open windows until the screen sessions are alle effectively closed is quite usefull!
The real luxury however is when you need to install the same software on several different machines. You start by opening ssh connections to the boxes you want to administer. You might consider using the .screenrc to open the connections and ssh keys to facilitate the loging in. Once logged in, you can send the appropriate commands (for example “sudo yum install htop”) to the different screen sessions, effectively managing all those machines at the same time. This is ofcourse something to use with extreme caution – imagine deleting the wrong directory on one of those boxes – but a conscient administrator should be able to use this to his advantage!
The articles in this series were updated to gertschepens.be; These articles focus on the following topics..