Using GNU Screen
Posted by Steve on Sun 17 Oct 2004 at 19:54
GNU Screen is an often overlooked application which allows you to run programs in a console section, detach from them and then later resume them. They even keep running when you logout.
Part of the reason why the program is often overlooked is because it has a very complicated description. The GNU Screen Homepage describes it as :
"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal... [snip]"
Whilst that's a very technically accurate description of the program it doesn't tell most newcomers exactly what the program does.
Essentially GNU Screen is a windowing system for the console.
When you run it inside an xterm, or a remote SSH login, you can create multiple new windows all in the same session, switch between them, or view two or more at once.
In addition to this a screen session is independent of your connection, so you can connect from one location, run things inside a screen session then detach. All the programs keep running and you can later reconnect and resume working right where you left off.
It's the attaching and detaching that makes screen so useful as it allows you to start long running programs, leave them running in the background and later on come back to them. This is very useful for things like console based IRC clients (irssi-text for example).
Lets start with an example which make it easier to understand what's going on.
If you run screen you will see the following text:
Screen version 4.00.02 (FAU) 5-Dec-03
Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program (see the file COPYING); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to
screen@uni-erlangen.de
[Press Space or Return to end.]
Press space and you will be presented with what looks like an ordinary shell window. To test this run a command or two, for example:
skx@lappy:~$ ls /tmp gconfd-skx keyring-9hZRfE mapping-skx orbit-skx ssh-MDkRIT1001 skx@lappy:~$
There you can see I've run the command ls /tmp, and you can see the output.
Now we get to see the power of the program. Press Ctrl+A, then d. You will see a message saying that your screen session has been detached, and will be back at your original prompt:
skx@lappy:~$ screen [detached] skx@lappy:~$
So what happened to your shell? Well it's still there! Simply run screen -R and you will be back at the session you detached from.
This shows you the basics of using the program, you've learnt how to run a copy of screen, run a command inside of it and detach from it (Ctrl+A d) then reconnect (screen -R).
This allows you to do a lot already, connect to a host from your office via ssh, start up screen and run your favourite text based IRC client. At the end of the day you can detach from it - leaving it stil there to return to from your home machine.
But we've only scratched the surface of what screen can do, remember we said that it could run multiple windows? Lets create another one.
Press Ctrl + a then c (for create). You'll have a new fresh shell session.
Toggle between the two shells you should have by pressing Ctrl + a then Ctrl + a again.
You should see that you're switching between two independent shell sessions. If you press Ctrl + a then " you'll see a list of the windows you have available to switch between.
This window list shows the window number on the left, along with the name of the window. By default the windows will be named after the shell you are running, for example bash.
To rename the current window use Ctrl + a then A.
When it comes to time to close a window you have two choices, you can either type exit in the shell session to close it which will close that window too, or you can use Ctrl +a k to kill the current window.
As well as using one window to switch between you can display two, or more, windows at the same time!
Use Ctrl + a and S (Capital 's' for split) and your window will split in two. You can move between the two halves of the window with Ctrl + a and TAB.
By default the new window will be empty, but you can switch another window into that place by using Ctrl + a repeatedly, as we've already covered.
To make it more obvious which window you're currently working in you can setup a small status bar across the bottom of the window which show you how many windows you have open, and which one you are working with.
To do this create a file .screenrc in your home area and place the following inside it:
# An alternative hardstatus to display a bar at the bottom listing the
# windownames and highlighting the current windowname in blue. (This is only
# enabled if there is no hardstatus setting for your terminal)
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
(You can override all the default settings in your local file, or update the global file in /etc/screenrc to affect all users).
To sum up we've seen that screen is a very powerful tool for executing commands that run for a long time, and running multiple programs all at the same time in a single session.
We've not covered all the options, just enough to get started with. Running man screen will give you lots more options.
Here's a list of all the commands we've covered:
- screen
- Run a new screen session
- screen -R
- Reattach to a previously detatched session
- Ctrl-a c
- Create a new window.
- Ctrl-a k
- Kill the current window - after confirmation
- Ctrl-a Ctrl-a
- Switch to the other window
- Ctrl-a S
- Split the current window in two.
- Ctrl-a TAB
- Move between split sections of the screen.
- Ctrl-a A
- Give the the current window a name.
- Ctrl-a "
- List all windows - move around to change the window with the arrow keys
[ View Steve's Scratchpad | View Weblogs ]
Thanks - I'm hoping that this will continue and that others will contribute content as time goes on.
-- Steve.org.uk
[ Parent | Reply to this comment ]
The -x flag is cool. Let's you attach a screen session to different places at the same time so you could walk somebody through something for example...
[ Parent | Reply to this comment ]
I use it for two things: teaching my minions how to admin Unix/Linux without them getting out of their seats (using the previously mentioned -x option), and just the flexibility of being able to continue from where you left off, from anywhere.
You can start a session via telnet from a windows box in the office, ctrl-a d out of it. Pick it up from a Sun box in the computer room, ctrl-a d out of it. Carry on from a WYSE terminal in the warehouse... carry on from a ssh session at home via VPN...
It really is very useful - the manual description doesn't do it justice - just try it.
If you have ever used the wonderful VNC program ( www.realvnc.com , www.tightvnc.com , like PCanywhere) you'll be familiar with the idea. Screen is a simpler, more efficient way of doing the same with text screens, with the advantage of multiple screens too.
Then again, VNC probably deserves a tip page of it's own...
[ Parent | Reply to this comment ]
I think you're right, vnc probably does deserve a writeup of its own, especially some of the newerver packages like x11vnc, or vtgrab.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
I posted a small piece now - remotely administering machines graphically, with VNC.
It doesn't cover all the available options but it's a good introduction piece.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
but wait -- there's more :)
if you ever need to ssh to another machine and run screen there while already in a screen session on the first (i.e., nested screens, if you will), you'll realize that CTRL+A can no longer be used as the 'magic' key for both screen sessions.
fortunately, the 'magic' screen key can be changed on a session-by-session basis, and can be set to any reasonable value. for example, using
screen -e^Vvto start your session will make CTRL-V the 'magic' key.
just make sure you use a reasonable key for the purpose -- CTRL-S, CTRL-X, CTRL-D are bad ideas ;)
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I found an usefull tip about the C-a S (split) command, you can adjust the size with C-a :resize n (n is the number of lines, also +n and -n to add or remove lines).
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Ctrl-a c app
to create a new screen, then the alias app takes me to the application server. I would also like that app alias to rename the screen appropriately, so *I* don't have to.
[ Parent | Reply to this comment ]
You can setup the name of the initial screen session with the -S or -t flags.
See the manpage for details.
There's also the "shelltitle" option you can use in your ~/.screenrc file.
I'm not sure if that helps if you wish to create new screens though...
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
screen -X title newtitle
This will change the current screen window title to "newtitle". You can use it outside or inside of screen. I don't think this will work if you have more than one screen running.
You can probably figure out how to re-alias ctrl-a c to run this command :)
HTH
[ Parent | Reply to this comment ]
<esc>kname<esc>\So, for example, if you are creating a shell script and want to change the window title to 'appserver', you could include
echo -ne '\ekappserver\e\\'in your script.
[ Parent | Reply to this comment ]
"I'm helping a friend in his programming studies, usually over the phone. We'll often use a shared screen session, so we can watch each other work on code. The sequence is: he creates a screen session, hits (assuming ^Z is your escape char) ^Z:multiuser on[RET]^Z:acladd piquan[RET] (where my username is piquan); then I run (from a normal, non-screen terminal) screen -x jrh/ (where his username is jrh)."
[ Parent | Reply to this comment ]
# From: Adam LazurI am not aware of any fix to this, except of course to `chmod u+s /usr/bin/screen' if you're not worried about setuid. Anyway, root can always access a multiuser session, provided the session owner adds it to the ACL.
# Date: Fri, 27 Sep 2002 11:57:06 -0400
"For full functionality, screen must also be setuid root to do multi-user. This is another permissions setup problem (on the named pipes this time) that making screen setuid root avoids. It's on my todo list to fix things to that a setgid utmp screen will work with multi-user screen "out of the box" in the future."
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ View Steve's Scratchpad | View Weblogs ]
Thanks - good to know people enjoy the site.
I'm hoping that more and more people will return and submit pieces of their own - otherwise I'll likely run out of inspiration sometime soon!
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
I have a small writeup http://anders0n.net/blog/?p=42
[ Parent | Reply to this comment ]
This is what I placed in my .bashrc
if [ -n "$SSH_CONNECTION" ] && [ -z "$SCREEN_EXIST" ]; then
export SCREEN_EXIST=1
screen -DR
fi
Can anyone let me know what I am doing wrong?
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
When many computers have the same screen session to be able to do stuff at the same time (like two people sitting at the same computer, but over SSH) (example)
PC1: screen -x "Project"
PC2: screen -x "Project"
The problem is here:
When you create a new window in the screen (CTRL+A + C), it appears to only be in the current instance. That means that if PC1 creates a new window, if PC2 does 'CTRL+A + N', a warning will pop saying that there are no other windows... So apparently, when two people are in a same session, only the main window is visible to all participants, not all the windows in the screen.
Is this a bug?
[ Parent | Reply to this comment ]
Thanks,
Maniraj Patri
[ Parent | Reply to this comment ]
Thanks for writing this wonderful guide :)
[ Parent | Reply to this comment ]
http://www.s5h.net/wiki/screen#Automation
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Great and very useful site here. I was very suprised by the sheer amount of content!
[ Parent | Reply to this comment ]