GNU Screen
Screen is a console application which will allow you to run programs and detach from them, effectively running them in the background while still allowing interaction. You could, for instance, start a text program in a terminal in X, kill X, and continue to interact with the program. This article will get you a better understanding of Screen.
Contents |
Basics
The default escape sequence for screen is control+a. I find this to be unnecessary, and have bound this to ` (backtick). This can be accomplished by editing ~/.screenrc:
escape `e
This will allow you to still enter a backtick by typing `e.
Basic commands include (if using a different escape sequence than control+a please substitute):
C-a 0 opens window 0
C-a A Rename the current window
C-a c Create a new window (with shell)
C-a S Split current region into two regions
C-a <TAB> Focus on next region
C-a <ESC> Enter Copy Mode (use enter to select a range of text)
C-a ] Paste text
C-a Q Close all regions but the current one
C-a d Detach from the current screen session, and leave it running. Use screen -r to resume
See also: Using Screen effectively
UTF-8
To enable utf-8, put the following line in .screenrc:
defutf8 on
or start screen with the -U flag.
Starting programs when screen is started
It is possible to start programs when screen starts, and assign window numbers to them. This is accomplished by adding the following lines to ~/.screenrc:
screen -t rtorrent 1 rtorrent screen -t ncmpc 2 ncmpcpp screen -t elinks 3 elinks screen -t irssi 0 irssi
In this example, rtorrent ncmpc++ elinks and irssi would start when you run screen. The variable after -t indicates the window title, the number is what window you want the program to run in, and the last variable is the executable name.
Detaching
One of the best features of screen is the ability to detach and re-attach to screen sessions. This enables you to do a multitude of things, including deatching from a screen session on a remote host and logging out, while leaving your programs running.
The sequences are as follows:
C-a d
Detach from a screen session
screen -r
When run from the command line, will re-attach to a detached session.
screen -x
Screen -x will allow you to connect to an attached screen, allowing multiple users to connect to one session. Very good for collaborations.
screen -dr
Will detach the running screen, then attach to it. Useful if you did not detach the session and do not wish to use screen -x.
Multiple screens and nested sessions
It is possible to run multiple instances of screen, or even to have nested screen sessions. For multiple screens,
screen -list
will list the names and pids of any screen sessions that are running. By using
screen -r {pid}
replacing pid with the output from screen -list, will allow you to attach the screen of your choosing.
With nested screens, that is a screen session running within a screen session, the escape sequences are:
C-a a d
to detach the nested screen, and
C-a a K
to kill it. Additional options are listed in the man pages.
Hardstatus line
It is also possible to have screen output certain status lines for your computer, including load, uptime, date/time, hostname, and several other options, all using color. The manual page for screen (man screen) contains all configurable variables. The code below will output the date, names/numbers of open windows, and the system load:
hardstatus alwayslastline
hardstatus string '%{bk}[%{w}%d.%m.%y%{b}][%= %{wk}%?%-Lw%?%{=b kb}(%{w}%n*%f %t%?(%u)%?%{=b kb})%{= w}%?%+Lw%?%?%= %{b}][%{w}%l%{b}]%{b}'
Starting at window one
Screen is odd in that the first window it creates it window 0. If you would prefer for the first window created to be 1, insert the following in .screenrc:
bind c screen 1 bind 0 select 10 screen 1 select 1
Auto-reattach your existing screen session on login
Placing a script in your ~/.bashrc file will execute it every time you run the bash shell. This simple script will look for your existing screen session when you connect. If one is found, it will detach a remote screen if one exists, and reattach. To add it to the file, simply copy from 'cat' to the last 'EOF' and paste it into your ssh session. Then reconnect to test it!
cat >> ~/.bashrc << EOF
if [ \$SSH_TTY ] && [ ! \$WINDOW ]; then #comment out for local usage
SCREENLIST=\`screen -ls | grep 'Att'\`
if (( ! \$? )); then
echo -e "Screen running and attached:\n \${SCREENLIST}"
else
screen -U -d -R
fi
fi #comment out for local usage
EOF
You can also comment out the noted lines to use the same script on non-ssh connections, i.e. your local machine.
x
Questions and Answers
Getting in and out
Start screen:
Type screen in the terminal
Detach from a screen session (session remains open):
Ctrl+a d
Reattach to an open screen session:
screen -D -R
or if that doesnt work:
screen -x
Quit screen completely (session closed):
Ctrl+a Ctrl+\
HELPFUL if you have multiple screens create new screen with a topic `screen -S [subject]` list screen session ids `screen -ls` attach to a specific screen session `screen -D -R [screenid]`
Q: How do I control windows in screen?
A: Here are windows commands on screen:
Create a new window with a shell and switch to it:
Ctrl+a c
List currently open windows for selection:
Ctrl+a “
Close current window:
Ctrl+a k
Switch to next window:
Ctrl+a n
Switch to previous window:
Ctrl+a p
Switch to window #n:
Ctrl+a n
Cycle through windows:
Ctrl+a Space Bar
Q: How do I control regions and what are regions?
A: Using regions allows you to display more than one shell on your terminal. Split the current region into 2 new ones (note: new region will be blank, you need to switch to it then select a window to display in it):
Ctrl+a S
Split the current region vertically into 2 new ones (note: new region will be blank, same as above)
Ctrl+a :split -v <return>
Switch focus to the next region:
Ctrl+a TAB
Remove the current region:
Ctrl+a X
Q: How do I make my screen session available on multiple computers?
A: Sometimes its beneficial to have the same screen session available from multiple computers. For example, to connect from home and from work at the same time into the same session. To do this, start your screen session like you normally would, and the de-attach it with:
Ctrl+a d
Now, reconnect again with:
screen -x
The next time you ssh in from another location, you can use the same command to connect to the same session.
See Also
Dotfiles has many examples of .screenrc.
The Archlinux forums have a thread about .screenrc and screenshots associated with them.