Screen
From Software By Jeff
Screen is a valuable tool to extend shell sessions. It provides some session security, preventing loss due to disconnection. It also allows multple shell sessions at the same time through the same connection, with simple switching and copying between each.
This should help make screen more useful from the start.
| Table of contents |
Starting Bits
For most of the modern Linux distributions, screen is probably installed by default. If not, it'll certainly be available in the distribution's software repository. If not, here's some links to get it and get it going.
The screen homepage: http://www.gnu.org/software/screen/
The screen manual: http://www.gnu.org/software/screen/manual/screen.html
Starting Screen
To simply start a new screen session when connected to a system with a terminal program (e.g., PuTTY or other SSH): screen
Even better, to reconnect to existing screens (or create a new one if it doesn’t exist): screen –DR
To see if you’re already running a screen: screen -list
Most of the time, screen –DR is the way to do it. Shell (SSH or TELNET) to a system, type screen –DR and you should be presented with a screen-wrapped shell session. Work there instead of in the raw shell. Then if you close the terminal window (accidentally or on purpose), the network drops, or any other flake occurs, the work is still in-progress on the remote system and waiting for your return.
Start-up Configuration
Create a file in your home directory named .screenrc and put these bits in there, and you’ll be able to tell when you’re in a screen. Other bits are described in the documentation, and hints follow.
shell -bash
shelltitle "$ |bash"
hardstatus on
hardstatus alwayslastline
hardstatus string ' [ %H ] %{wb} %c:%s | %d.%m.%Y %{wr} Load: %l %{wb} %w '
defscrollback 50000
The options in the file are plentiful, especially when looking for what the hardstatus bits mean. http://www.gnu.org/software/screen/manual/screen.html#Command-Summary
Working with screen
These are the used-constantly bits. Much, much more available in the documentation. http://www.gnu.org/software/screen/manual/screen.html#Default-Key-Bindings
In screen, the default hot-key is Ctrl-a. You can change that in the .screenrc file if you want to (see the "escape" option in the Command-Summary page above). To be clear on notation, "Ctrl-a a" means to hold control, tap a, release control, tap a again, of course.
To send hot-key to your program (instead of letting screen eat it): Ctrl-a a
To exit screen, there are a few options. These are very frequently used:
- To hard-kill all open screens and end this screen session (no longer running): Ctrl-a Ctrl-\
- To disconnect from screen (stays running) and stay in your shell: Ctrl-a d
- To disconnect from screen (stays running) and close your shell (power disconnect): Ctrl-a DD
That last is useful when you’re using PuTTY or SSH to connect to the server, start screen, and then don’t need the shell again—PuTTY or the terminal running SSH will close. The alternative is that you’ll exit screen, and still be in the shell, so you’d have to exit that shell to close PuTTY or end the SSH session. If you’re on a Linux box, SSH’d to another box, you’ll have to open the terminal, SSH to another box, be at that prompt, enter screen…and exit each of those; power disconnect closes that first terminal! It does leave the screen running, so when you return to that server and restart screen (with –DR) it’ll reconnect and leave you where you were.
- To clear the screen when in screen: Ctrl-a C
- To create a new screen in screen: Ctrl-a c
An evil little bit of confusion there as older screen is still there, but dirty, but it looks like it cleared because of the new screen that appears.
When working with multiple screens:
- To switch to “next” screen: Ctrl-a n
- To switch to “previous” screen: Ctrl-a p
- To exit a screen in screen: Ctrl-d (or type exit) – exiting the last screen will exit screen
- To help keep track, you can change the title for a screen (from the "bash" in the .screenrc example above): Ctrl-a A
One evilness in screen is that the scrolling in your terminal window is disrupted; no more useful scrollbars. Because screen is maintaining its buffer internally, the terminal scroll window doesn’t really have much to do with the scrollback buffer. Curiously, even though it does this, it offers no navigation mechanism! One easy trick to navigate around in screen is switch to copy-mode and use the arrow keys instead of the mouse.
- To enter copy-mode: Ctrl-a [
- To mark a block, hit enter to start, navigate, and hit enter again to end.
- To exit copy-mode without affecting the clipboard, hit escape.
Another benefit screen gives in this area is that if you resize the window, the screen contents resize with it. This can get a little quirky if you resize while running an interactive program (like vim) because that program will have dealt with the resizing, but the shell underneath will not. Quick cure when that funkiness becomes apparent is to resize the window again (even if it’s just grab the frame and drop it).