Screen

From Just another day in the life of a linux sysadmin
Jump to navigation Jump to search

Screen Basics

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

Screen is a terminal program that allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from the command line, and for separating programs from the shell that started the program.

Similar to VNC, screen allows the user to start applications from one computer, and then reconnect from a different computer and continue using the same application without having to restart it. This makes migration between two different workstations simple.

Screen also allows multiple computers to connect to the same session at once. Allowing multiple users to connect to the same session allows one person to follow along with another person working on a server.

http://www.gnu.org/software/screen

Basic Commands

Screen is started by using the screen command:

screen

flags

Some common flags you can use are as follows:

flag description example
-ls List current screen sessions. screen -ls
-aS Create and names a screen session which include all capabilities screen -aS screenname
-r Resume a detached screen session. screen -r (name|pid)
-x Attach to an attached screen session. screen -x (name|pid)
-d Detach an attached screen session. screen -d (name|pid)
-d -m Start an new screen session in detached mode and run $command. screen -d -m $command
-DR Force disconnect of current user and reconnect with your user screen -DR
-wipe Removes all dead screen sessions. (Sessions that are unreachable.) screen -wipe
-dmS Autoclose a screen session after command completes screen -dmS maldet_is_still_running maldet -a /hom?/?/public_html/

key bindings

While inside the screen session ctrl+a activates the key binding. The following table shows some of the default key bindings:

key binding
? Show key bindings.
A Set a title for the current window.
c Create a new window.
n Switch to the next window.
p Switch to the previous window.
0-9 Switch to window number n.
w Show a list of windows.
" Present a list of windows for selection.
F Resize the window to the current region size.
[ or esc Enter copy/scrollback mode.
k Destroy current window.

Examples

Get the screen list:

screen -ls

You will then see something like this:

There is a screen on:
        22211.ttyp0.host        (Attached)
1 Socket in /tmp/screens/S-root.

This shows you the currently running screens. As you can see by the "(Attached)" bit, 22211 is currently attached to a terminal.

Since this screen is currently attached, you can remote detach it with this command:

screen -d 22211

If someone else is logged into the screen session and actively working this will kill their session, so don't just go all willy-nilly and start detaching screen sessions.

To reattach to that screen, run this command:

screen -r 22211       

You can detach and reattach the screen at the same time:

screen -d -r 22211

To create a new screen session called ScreeenName, enter this:

screen -S ScreenName

Changing screens behavior

scrollback

To make scrollback work with xterm, add this to /etc/screenrc/ on your workstation:

#make xterm behave with scrollback in screen
termcapinfo xterm ti@:te@

Multiuser Screen Sessions

Multiuser is not on by default in screen for security reasons.  So in order to allow multiple users to connect to the same screen session, multiuser must be turned on.  Below are the steps needed to do this.

 1.Start with logging in and starting screen on the command line. 
 2.Then type Ctrl-a to enter the command mode, 
 3.Next type : (this is a colon) to enter the screen's build-in command line. 
 4.You can now activate the multiuser mode by entering 
multiuser on
in the screen's command line that appears in reverse color at the bottom of the console window. 
 5.To execute, hit Return. 


Multiuser mode can be useful if you are attempting to show a customer what you are doing on a server. With this mode on the customer will be able to follow your keystrokes within that screen session.

Documentation

If you need more in depth information than this consult the man page:

man screen

Or check out this screen wiki.

Extras

Start a screen running named ASDF and run top in it, without attaching, nothing will appear to happen but you will see the screen with screen -ls:

screen -S ASDF -d -m top

Kill the ASDF screen session:

screen -S ASDF -p 0 -X kill

Start a bash session:

screen -S ASDF -d -m /bin/bash

Execute ls inside the screen session:

screen -S ASDF -p 0 -X exec ls /home

Kill the ASDF screen session:

screen -S ASDF -p 0 -X kill


Scroll up in screen window:

CTRL+a then ESC #Press CTRL KEY and 'A' KEY together, then the ESC KEY.
#scroll around with arrow keys, ESC again to exit.

Here's a quick list of the most useful commands that you might need to use:

Template:Notice

* Create New Screen: Ctrl+A, C
* Switch to Screen: Ctrl+A, N (where N is the number of the screen)
* Switch to Next Screen: Ctrl+A, A
* View List of Screen: Ctrl+A, W
* View Screen Picker: Ctrl+A, " (double quote)
* Detach Current Session: Ctrl+A, Ctrl+D
* Attach to Running Screen Session: screen -R
* Show Shortcut Keys: Ctrl+A, ?

There's loads of other commands that you can use to control your screen session, most of which can be accessed by typing Ctrl+A, ? at the prompt, which will bring up a help window that shows you all of the available bindings:

* 'C-a p' and 'C-a n' can be used to switch to the next or previous window respectively.
* 'C-a N' - where N is the number from 0 to 9, that can be used to jump to the corresponding window.
* 'C-a w' displays a list of all windows. The unique ID of each window with its name and running process is displayed, for each window. The current window is marked with an asterisk(*).
* 'C-a k' - can be used to kill the current window. You can also type 'exit' to kill the current window. If no more windows are open, then screen exits. 'C-a \' also does the same thing.
* 'C-a d' - detaches the present screen session. 

You can also detach by closing the terminal running your screen utility. Although this appears to close your terminal session, in reality this does not happen. It only unbinds your session from the current terminal. All the programs started under screen will still keep running.

You can also log out from the machine and re-login. Then start any terminal session and type 'screen -r' to once again be connected from where you left. In case, there were more than one screen sessions running on the machine, Screen prompts for a host.tty.pid. For example, say I have two screen sessions. So when I type 'screen -r' command, it gives the following message: $ screen -r There are several suitable screens on:

5545.pts-6.localhost  (Detached)
8412.PTS-6.localhost  (Detached)

To connect to one of the above detached screens,run

screen -d -r 5545.pts-6.localhost

or you can just use the first numbers

screen -d -r 5545

or screen -x for an attached screen