Screen - Virtual Terminals From Console

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

screen

screen [options] [command [args] ] 

In their words: Provide ANSI/VT100 terminal emulation, making it possible to run multiple full-screen pseudo-terminals from one real terminal, and letting you manipulate and save your screen input and output, copy and paste between windows, etc.

In other words: multiple virtual terminals from one terminal (like a remote shell), with the ability to detach processes and leave them running even when you disconnect!

  • Do more from one terminal connection
  • Leave stuff running even if you detach or disconnect
  • Handy for unreliable terminal connections
  • Scroll back and see what you missed
  • Always use screen and never loose your work by an interrupted Internet connection!!!

Screen offers the ability to detach from a session and then attach to it at a later time. When detached from a session, the processes screen is managing continue to run. You can then re-attach to the session at a later time, and your terminals are still there, the way you left them.

Screen also maintains individual, searchable scrollback buffers for each of the windows it manages. Screen sends all entered text to the current window, with the exception of the command character. The default command character is Ctrl-A. .screenrc is the per-user configuration file in your home directory, and /etc/screenrc is the system-wide configuration file that applies to all users.

Screen sets TERM to screen—each screen window provides its own vt100-compatible virtual terminal. The variable WINDOW is set to the virtual window number, and the variable STY is set to your session name.

Using Screen Walk Through

1. Telnet or SSH into a remote Linux system with screen installed.

2. Type 'screen' in the shell and press enter.

3. CTRL-A + C will open a new screen virtual console.

Say you had 'top' running in the first console, you can press CTRL-A and top will remain running in console 1, it will not be a stopped job, nor will it be 'background' it will in fact remain a fully interactive running process. It is just like you opened a second connection with the remote system.

4. CTRL-A + P will return you to the previous virtual console which is running the top command.

5. CTRL-A + N will put you into the second virtual console

P for previous, N for next.

Basic Screen Commands

CTRL-A +

C   new console
P   previous console
N   next console
M   monitor for activity
_   monitor for inactivity (default 30 sec)
"   list of open screen consoles, use the J and K keys to navigate
Shft A   manually set screen names (label them)
K   kill a screen virtual console
\   kill all screen virtual consoles
D   Manually detach from session (same as disconnecting while screen is running)
]   Paste copied text from other session

Screen Sessions

This is the real beauty of screen. You are working within your virtual console, very busy, and your terminal session is lost, disconnected! With screen you can log back into the remote system and pick up where you left off!

Starting 'screen' without command line arguments starts a new screen session.

screen

After reconnecting to your shell, you can find and control existing sessions

 screen -ls
  • lists currently running sessions
 screen -r sessionname
  • attatch to a specific session by name
 screen -x -r sessionname
  • attatch to a specific session by name again allowing multiple attachments to a session
 screen -R
  • reattach if it can, or start a new session.
 screen -D -RR
  • detach defunct client and attach current to first available session

Example:

SSH into a remote shell where we left screen sessions running detached.

At the command prompt we type 'screen -ls' and see something like the following:

[user@host home]$ screen -ls
There are screens on:
        26942.pts-0.host    (Detached)
        4938.pts-0.host     (Detached)
        5361.pts-0.host     (Detached)
3 Sockets in /tmp/uscreens/S-home.

So we want to attach to one of the sessions and pick up were we left off. Lets attach to the first one in the list by typing 'screen -r 26942'. Notice it is not necessary to enter the .pts-0.host portion.

The Screen Scrollback Buffer

  • enter copy mode with Ctrl-A [ or via the copy command
  • Arrow keys and Page Up/Down navigates. Vi nav also works.
  • / and ? for vi-style search
  • Ctrl-S and Ctrl-R for incremental search
  • spacebar to mark text to copy and to mark copy end point
  • Ctrl-A ] to paste copied text into other session

screenrc

When screen is invoked, it executes initialization commands from the files `.screenrc' in the user's home directory and `/usr/local/etc/screenrc'. These defaults can be overridden in the following ways: For the global screenrc file screen searches for the environment variable $SYSSCREENRC (this override feature may be disabled at compile-time). The user specific screenrc file is searched for in $SCREENRC, then `$HOME/.screenrc'. The command line option `-c' specifies which file to use.

The text scrollback buffer in screen is limited to 100 lines by default. Adding the following to the screenrc file will increase that to a reasonable amount:

 defscrollback 1024