Screen Notes

From blag.wiki.aktivix.org

Jump to: navigation, search

SCREEN(1) These notes where made from the man page of `man screen` using the jed editor. This is completely unofficial in that I have no affiliation with the authors of the original document. Please tell the guys in the blag forums at http://forums.blagblagblag.org if you see any glaringly obvious mistakes. I've put this doc in the public domain so that others can learn about this truly great tool. John Maclean <info *at* jayeola &dot& org>


Contents

NAME

Screen is a console, text-based application that allows one to have multiple terminals within the same running terminal.


OVERVIEW

You log into to a virtual terminal or bring up a shell terminal such as xterm. Run screen within that same shell session to run processes and commands within that first shell without using another terminal. So log in once and have as many sessions as required. It was created during the days when people would log into remote machines over slow connections. Each connection and log in would take some time (password authentification and stuff) and so screeen was designed to allow a user to have multiple terminals within the same login session but not have to dial up to the remote box in order to have a new shell.

WHAT'S THE POINT?

  • You may be working on a slow connection, (e.g. dial-up or wireless).
  • You can start a screen session and detach it. Once detached the process(es) are still running even though you have logged out of the machine. Run make in a screen session, detach from it and the command is still being run when you detach and log out from the remote machine.
  • Detached screens can be reattached so that you can resume that shell session at any time.
  • You can copy and paste commands, output and text between screens.
  • Multiple users can be logged into the same session to allow group participation.

PREPARATION

To find out if you have it installed and its full path run as a normal user;

type screen

if it's installed you'll see output similar to

screen is /usr/bin/screen

showing you where it's installed. Use your package manager of choice to install it;

apt-get -y install screen

or use synaptic, yum or some other thing but you should install it as user root. If you are having trouble installing it you could query the repository;

apt-cache show screen

You'll see output similar to the following;

Package: screen
Priority: optional
Section: misc
Installed-Size: 980
Maintainer: Adam Lazur <zal@debian.org>
Architecture: i386
Version: 4.0.2-4.1
Depends: libc6 (>= 2.3.2.ds1-4), libncursesw5 (>= 5.4-1), libpam0g (>= 0.76), base-passwd (>= 2.0.3.4), passwd (>= 1:4.0.3-10)
Pre-Depends: debconf (>= 0.2.17)
Conflicts: suidmanager (<< 0.52)
Filename: pool/main/s/screen/screen_4.0.2-4.1_i386.deb
Size: 581702
MD5sum: 69be0f4a8d612f598855d34b72ceb431
Description: a terminal multiplexor with VT100/ANSI terminal emulation
 screen is a terminal multiplexor that runs several separate "screens" on a
 single physical character-based terminal.  Each virtual terminal emulates a
 DEC VT100 plus several ANSI X3.64 and ISO 2022 functions. Screen sessions
 can be detached and resumed later on a different terminal.
 
 Screen also supports a whole slew of other features.  Some of these are:
 configurable input and output translation, serial port support, configurable
 logging, multi-user support, and utf8 charset support.

SYNOPSIS

These are the various ways to start the command;

screen [ -options ] [ cmd [ args ] ]
screen -r [[pid.]tty[.host]]
screen -r sessionowner/[[pid.]tty[.host]]


DESCRIPTION

When you run screen you'll see a normal shell with which you use as you normally would any other terminal. That shell is actually a "window" that will allow you to run other shells within it. Each new screen created is another shell. There is a full set of commands to operate on the "screen windows" that you create and you can produce as many screens as your machine can handle. Operations on screens include:-

  • Creating more screen windows to run more commands
  • Running commands within a new window
  • Listing all active windows
  • Controlling windows
  • Sending output from one window to another
  • sending commands to the background
  • Killing windows to stop their processes
  • Logging output of one or more screens
  • Copy-and-paste text between windows
  • Scroling and history
  • Switching between them
  • Detaching from a screen for reattachment to another machine
  • ... and then some

The advantage with running screen in a single terminal is it's ability to hide the windows so that you only see one at a time. (This behaviour can be changed). Each window is independent of any others created to that their processes are mutually exclusive. Window 0 can run processes completely seperate from window 5.


WHAT DOES IT LOOK LIKE?

You'll see your normal terminal but with a slight twist. New windows are created by sending existing ones "behind" the new screen that you've called.

#------------------------------------------------------------
Fig 1: A number of active screens "stacked" behind each other.
#------------------------------------------------------------
        X-------Screen 2---------------------------------X
    X-------Screen 1---------------------------------X   o
X------------------------------------------------X   o   o
o                                                o   o   o
o                                                o   o   o
o                                                o   o   o
o   Screen 0  This window is visible to the      o   o   o
o             user. The others have active       o   o   o
o             processes and are chilling out     o   o   o
o             behind this one                    o   o   o
o                                                o   o   o
o                                                o   o   o
o                                                o   o   o
o                                                o   o---X
o                                                o   o 
o                                                o---X
o                                                o
X------------------------------------------------X

Each window shown in Fig 1 represents a screen running a process. The processes and screens are mutually exclusive so that killing screen 0 would stop jobs in that screen and that one alone. Killing a screen would bring any other screens that were "hiden" in the background to the foreground. If there was only one active screen window then killling that would end your screen session.

Since each screen window is associated with a process everything that you type is sent to the window of the active one, which is normally the one that you can see right now. There are commands that control windows "globaly" including those that can't be seen but we'll come to that later.


KEYBINDINGS INTRO

To send commands to the screen window manager use the keys;

<control> a <command>

and from hereon that key stroke will be refered to as C-a. The command can be another screen keystroke. They can be customized to suit your preferences but it's best to get used to using them "out of the box" with the default keystrokes so that you can get used to using them. This app does not consider <control> as control but it's own kind of signal for a screen window command. The docs refer to control as ^ so we will use that notation here.


GETTING STARTED

Once it's installed the simplest way to start a screen session is to type.

screen

and that's it. To create another screen and send the active window to the background you can either type 'screen again or

C-a c

which is the notation for control a simultaneously immediately followed by a c.

You should now have two active screens windows although you'll only be able to see one. The first one that you created was sent into the background when you called the second screen.

There's not much to screen if you are familiar with terminals. All we are doing is creating an almost umlimited number of shells complete with our normal prompt. Since we are logged into the box we don't need to supply a username and password to create the new terminals. To actually do something useful one can run;

screen irssi -c irc.indymedia.org

to have your first screen window run irssi, the irc client and have it connect to indymedia's irc server. You could then hit C-a c and when that new screen window appears you'll notice that your irc sesion has disappeared. It's actually behind the new screen that you have just created. I like using the jed editor. So for my second screen window I'll just type

jed /path/to/some/file.rb

and I'll be able to edit the file file.rb.

Note that the numbering of all active screen windows starts at zero so screen 0 is the first screen, screen 1 is the second and so on.

There's also another way to produce new screen windows. Use the keystroke;

C-a: <command>

where <command> is a command sent to the screen program. So in this case you could run;

C-a: emacs -fg red /path/to/some/file.py

To have emacs edit a python file with a red foreground. Remember that all active screens are sent to the background once you create a new screen. So in this case the irc session and the file being edited by jed are still running but are sent behind the new screen that's editing the python file with emacs.

All of this default behaviour is configurable but we'll get to that later. Better to understand the program and get to grips with it, eh? ;)


TERMINAL SETTINGS

Screen should be ready to work on your machine as your distro will most likely determined it's terminal settings. I've not looked into the termino/termcap settings. It's always worked stright out of the box for me but you could investigate the following man pages if you have any problems;

man terminfo
man termcap
man env
man tset

to understand more about your terminal and general environment.


THE ONE COMMAND TO KNOW

This is the help keystroke;

C-a ?

You'll see a new screen appear with a summary of commands and their assocciated key strokes. Use the spacebar or return to kill that help window and return to the one that you were using before.


COMMAND-LINE OPTIONS

These are the options available for version version 4.00.02 (FAU). There are loads of them but you don't have to be familiar with most of them to be able to use it effectively. The options that I consider to be useful have been discused briefly below. For more info read the man page or try info screen.

-c file

Use another config file intead of the one that may be in "$HOME/.screenrc"


-d -r

Reattach a session and if necessary detach it first.


-d -R

Reattach a session and if necessary detach or even create it first.


-d -RR

Reattach a session and if necessary detach or create it. Use the first session if more than one session is available.


-D -r

Reattach a session. If necessary detach and logout remotely first.


-D -R

Attach here and now. In detail this means: If a session is run-ning, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author's favorite.


-R

Attempts to resume the first detached screen session it finds.


-t name

Give a process a title


-v

Print version number.


-x

Reattach to a not detached screen session. All of the existing process running in that screen session are still running even though you have been detattatched from it. Use this to connect back to that session.


DEFAULT KEY BINDINGS

Remember to use C-a <command> for the following, where <command> is usually another kystroke. [Tip:- Help is C-a ?]

C-a '

Chose to enter this window when you have one or more to chose from.

                                to.
C-a "

Create a windowlist. Each one is numbered from zero. Use the <up> and <down> keys to select the process or hit a number to enter it.


C-a <single digit>

As above but select a window from a list by enering a single digit.


C-a - <number>

Switch to window <digit>


C-a tab

Change focus to the next available region within the same active window.


C-a A <title>
Get a prompt so that you can give your active windows meaningful names.

I tend to name window after the process that I'm running it it.


C-a c

Produce a new window with a terminal shell.


C-a C

Clear the screen.


C-a d
C-a C-d

Detach screen from this terminal. This is a very handy feature. You detach the screen but all processes are still actiive. This allows you to log out of a box and login later. All of the processes within any running screens are active and are under your control again when you reattach to the session.


C-a D D

Similar to above but you have logged out of the machine. Active processes are still running.


C-a C-g

Turn the visual bell on or off. The creen "blinks" on errors. May be a bit annoying for some users so you can turn if off.


C-a h

Copy the contents of the current screen wxx to a file called "hardcopy.n".


C-a H

Write a log of for the current window to a file called "screenlog.n".


C-a i
C-a C-i

Get info about a window. See its name, number and other stuff.


C-a k
C-a C-k

Kill/destroy a window. Use this to immediately terminate any processes running within it.


C-a l
C-a C-l

Redraw the current window.


C-a m
C-a C-m

Show me the last message relating to screens. Includes error messages.


C-a space
C-a n
C-a C-n

Switch to the next window.


C-a N <digit>

Show the number (and title) of the current window.


C-a backspace
C-a h
C-a p
C-a C-p

Switch to the previous window in the list and make it active. All others are sent to the background.


C-a Q

If you have more than one region within a window, make the one that you are in now the only one. The other regions are turned into windows if they had a seperate process and no processes are killed.


C-a r
C-a C-r

Turn the live wrapping of a window on or off depending on its current state.


C-a s
C-a C-s

Lock the keys for the current window.


C-a S

Split the current region into two new ones. Very handy! see C-a <tab> below


C-a t
C-a C-t

Show system time and cpu load.


C-a v

Show screen's version info.


C-a w
C-a C-w

Show list of all open wxxs.


C-a x
C-a C-x

Locks the terminal.


C-a X

Kill the current region.


'C-a ?

Show a list of key bindings. This is the one command to know ;)


C-a C-\

Quit this session and kill all windows and their associated processes.


C-a : <comand>

The screen command line.


C-a [
C-a C-[
C-a esc

Initiate the copy mode. Use to copy text from one window or region to another. Very handy.


C-a ]

Initiate the paster mode. Use to paster text from one window or region to another. Very handy.


C-a {
C-a }

This is for copying and pasting entries from the command line.

CUSTOMIZATION

There's alot of customisation possible with this program. It's all done by copying the system-wide config file to your home directory. Don't edit the main file, edit the one that you copy under your home dir.

cp /etc/screenrc ~/.screenrc

You can now edit ~/.screenrc to your hearts content. Remember that you can override this file with

screen -c /path/to/alternative.configfile

to use another configuration file for screen.

Have a look at /etc/screenrc and /etc/etcscreenrc. At least one of them should exit exist. Copy one of them to your home diirectory as shown above. Note the dot . before the name of the file. It's to specify tha we are hiding the file from nornal file listings such as ls'. To see this file listed you'll have to run ls -al for example, to see all of the hidden files within this dir.

wc -l ~/.screenrc
91 /home/jayeola/.screenrc

This means that this file is 91 lines long, so it's not too long for a general read to get yourself familiar with (some of) the options. You'll quickly see that you set your keybindings and options are here. That's why it's recommended for each user on a system to have his/her own screenrc file within their ~/ (home dir). This will ensure that each user can have their own set of key bindings and options without conflicting with those of other users logged into the system.

I'm still a relatively new comer to screen so the only thing that I have changed in ~/.screenrc is the following line;

startup_message off

which turns off the start up message. I've seen it enough times to know how to get help ;).

There's also the opportunity to have screen fire up programs within other windows when it's fired up. This file ain't too long so get to know the syntax so that you can have your editor edit a file, have irc log into irc.indymedia.org, run top to monitor the system, use mplayer to play some music and all in seperate screen windows.

screen -t editor jed
screen -t sysmon top
screen -t irc irssi -c irc.indymedia.org
screen -t mplayer ~/media/mp3/foo.mp3

If you do edit and change ~/.screenrc then be aware of the existing syntax within this file. You'll be warned about any errors when you start screen if this is the case. You'll also be able to use varibales within ~/.screenrc but the syntax will be dependant on your version of screen.


RUN TIME COMMANDS

Hit

C-a : <command> to pass commands to a running screen session.

Use this to allow remote users to haave access to this current screen session.

acladd usernames [crypted-pw]
addacl usernames


aclchg usernames permbits <list of users>
chacl usernames permbits list <list of users>

Change permissions for a list of users. The syntax is in the form of <Permission bits> represented as `r' for read, `w' for write and `x' for execute. The user list is in the form of user1, user2,..

There are more commands relating to multi-user sessions. A short summary

is listed below;
acldel username

To remove a user from the screen sessions access control list.


aclgrp username [groupname]

Use this command to specify users that can access rights to the screen session.


aclumask [[users]+bits |[users]-bits .... ]
umask [[users]+bits |[users]-bits .... ]

Use this command to specify users that can access rights to the screen windows.


activity message

This is relates to the messages about any windows that are being monitored. Use the keystoke C-a M to turn monitoring on.


chdir <[directory>

Use this to change the current directory of the current screen session. If no dir is supplied it defaults to ~/. i.e. $HOME. All new screens created will have the directory specified as the current working directory unless specified by your ~/.screenrc file. Note that all screen sessions started from an arbitrary directory will use the value of $PWD when creating new screen windows.


clear

Clears the current window and saves its image to the scrollback buffer. Don't ask me what this means as I've not used it yet.


colon [prefix]
C-a : <command>

This is a method passing commands to screen and modify any ~/.screenrc options or keybindings on the fly. You can use the <up> and <down> arrows to run though the most recent commands. (Bit like a command history).


copy
C-a [

Let's you copy text and paste it to another place within the same window or another one. You'll see an annoucement in the message line at the bottom of the window imilar to "copy mode entered". Use Vi-like keys to move around the screen, select text and to place it elsewhere. Key movements:-


h cursor left
l cursor right
k cursor up
j cursor down
0 start of line
$ end of line
0 start of line
^ start of line
$ end of line
g start of buffer

There are more keybindings available so refer to the screen man page for more details.

Use this info when copyig text from one window to another

Marking:
The  copy range is specified by setting two marks. The text between these marks will be highlighted. Press space to set the first or second mark respectively.
Y and y used to mark one whole line or to mark from start of line.
W marks exactly one word.
Repeat count: Any of these commands can be prefixed with a repeat count number by pressing digits 0..9 which is taken as a repeat count.
Example:  "C-a  C-[  H  10 j 5 Y" will copy lines 11 to 15 into the paste buffer.
Searching:
/ Vi-like search forward.
Vi-like search backward.
C-a s Emacs style incremental search forward.
C-r Emacs style reverse i-search.
exec [[fdpat] newcommand [args ...]]

PACKAGE DATA

See Screen.

Personal tools