Anything Else

Thursday, December 30, 2004

Screen Howto

GNU-Screen is an immensely useful tool, that lets you manage sessions when working with remote linux/unix servers.

To start a fresh screen session:

$ screen

Once in the screen session, you can use screen commands to manage various screens. All screen commands start with Ctrl-a.

To Open a new screen within the screen session:

Ctrl-a Ctrl-c.

To switch:

Ctrl-a Ctrl-a to last accessed screen. Ctrl-a Ctrl-n to next, Ctrl-a Ctrl-p to previous screens.

To disconnect, leaving the session running in background: Ctrl-a Ctrl-d.

Full list of available command can be obtained by doing Ctrl-a Ctrl-?

To connect to an already open session running in background, use "screen" with following options:

  • -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 running, 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.
  • -D -RR Attach here and now. Whatever that means, just do it.
[[Note: It is always a good idea to check the status of your sessions by means of "screen -list".]]

My preferred option is screen -d -RR [put an alias for that in your .bashrc maybe]. If more than one person is using the same login on some machine and both are using screen, it may be advisable to use caution when doing this.

Other cool thing to play with is: Ctrl-a Ctrl-S [capital S] to split the screen vertically in half, Ctrl-a Ctrl-Q to unsplit and Ctrl-a Ctrl-Tab to move focus to other side of split.

Also to list the currently open screens in a screen sessions, Ctrl-a Ctrl-", and to rename the current screen to a more meaningful name do a Ctrl-a Ctrl-A

With all this Ctrl-a business if you miss sending Ctrl-a to the running program(for example to jump to the beginning of the readline buffer used to edit the commands we are passing to bash), do a Ctrl-a a.

You can also instruct screen to monitor a screen for silence by Ctrl-a Ctrl-_ [underscore], good to know when a program has stopped running in one of the hidden windows.

Now I am sure it would be hard for you to live without screen. :-)

One minor drawback: screen retains the DISPLAY setting of the terminal where it was first created, so if I want to quickly launch emacs etc, I detach, run the program and reattach again.

PS: Had written this for my company documentation! K5 has another tutorial about it. Another overview on screen here.

Labels: Invented Here Tips n Tricks

If you find this post useful, please conside buying me a pizza!

0 Comments

<< Home