Difference between revisions of "GNU Screen"

From ArchWiki
Jump to: navigation, search
m (Top: Removed unnecessary sentence, people probably know what to expect from the wiki.)
(Adjust category per Talk:GNU Screen, add whitespace)
Line 1: Line 1:
[[Category:System administration]]
+
[[Category:Terminal emulators]]
 
[[ru:GNU Screen]]
 
[[ru:GNU Screen]]
 
[[zh-CN:GNU Screen]]
 
[[zh-CN:GNU Screen]]
Line 12: Line 12:
  
 
== Usage ==  
 
== Usage ==  
 +
 
Commands are entered pressing {{ic|ctrl+a}} and then the key binding.
 
Commands are entered pressing {{ic|ctrl+a}} and then the key binding.
  
 
=== Common Commands ===
 
=== Common Commands ===
 +
 
* {{ic|ctrl+a}} {{ic|?}} Displays commands and its defaults (VERY important)
 
* {{ic|ctrl+a}} {{ic|?}} Displays commands and its defaults (VERY important)
 
* {{ic|ctrl+a}} {{ic|:}} Enter to the command prompt of screen
 
* {{ic|ctrl+a}} {{ic|:}} Enter to the command prompt of screen
Line 32: Line 34:
  
 
=== Command Prompt Commands ===
 
=== Command Prompt Commands ===
 +
 
* {{ic|ctrl+a}} {{ic|:quit}} Closes all windows and closes screen session
 
* {{ic|ctrl+a}} {{ic|:quit}} Closes all windows and closes screen session
 
* {{ic|ctrl+a}} {{ic|:source ~/.screenrc}} Reloads screenrc configuration file (can alternatively use /etc/screenrc)
 
* {{ic|ctrl+a}} {{ic|:source ~/.screenrc}} Reloads screenrc configuration file (can alternatively use /etc/screenrc)
  
 
=== Named sessions ===
 
=== Named sessions ===
 +
 
To create a named session, run screen with the following command:
 
To create a named session, run screen with the following command:
 +
 
  $ screen -S ''session_name''
 
  $ screen -S ''session_name''
  
 
To prints a list of ''pid.tty.host'' strings identifying your screen sessions:
 
To prints a list of ''pid.tty.host'' strings identifying your screen sessions:
 +
 
  $ screen -list
 
  $ screen -list
  
 
To attach to a named screen session, run this command:
 
To attach to a named screen session, run this command:
 +
 
  $ screen -x ''session_name''
 
  $ screen -x ''session_name''
  
Line 65: Line 72:
  
 
== Tips and tricks ==
 
== Tips and tricks ==
 +
 
=== Change the escape key ===
 
=== Change the escape key ===
 +
 
The escape key can be changed with the {{ic|escape}} option in {{ic|~/.screenrc}}. For example:
 
The escape key can be changed with the {{ic|escape}} option in {{ic|~/.screenrc}}. For example:
 
{{bc|escape ``}}
 
{{bc|escape ``}}
Line 73: Line 82:
  
 
=== Start at window 1 ===
 
=== Start at window 1 ===
 +
 
By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, add the following lines on your configuration:
 
By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, add the following lines on your configuration:
 +
 
{{hc|~/.screenrc|
 
{{hc|~/.screenrc|
 
bind c screen 1
 
bind c screen 1
Line 80: Line 91:
 
screen 1
 
screen 1
 
}}
 
}}
 +
 
=== Nested Screen Sessions ===
 
=== Nested Screen Sessions ===
 +
 
It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to {{ic|ctrl+a}} commands. To send a command to the inner screen session, use {{ic|ctrl+a}} {{ic|a}}, followed by your command. For example:
 
It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to {{ic|ctrl+a}} commands. To send a command to the inner screen session, use {{ic|ctrl+a}} {{ic|a}}, followed by your command. For example:
 
* {{ic|ctrl+a}} {{ic|a}} {{ic|d}} Detaches the inner screen session.
 
* {{ic|ctrl+a}} {{ic|a}} {{ic|d}} Detaches the inner screen session.
Line 118: Line 131:
  
 
=== Informative statusbar ===
 
=== Informative statusbar ===
 +
 
The default statusbar may be a little lacking. You may find this one more helpful:
 
The default statusbar may be a little lacking. You may find this one more helpful:
 +
 
{{hc|~/.screenrc|<nowiki>
 
{{hc|~/.screenrc|<nowiki>
 
hardstatus off
 
hardstatus off
Line 126: Line 141:
  
 
Another possibility, taken from [http://www.fordfrog.com/2012/09/02/71/ frodfrog's blog] is:
 
Another possibility, taken from [http://www.fordfrog.com/2012/09/02/71/ frodfrog's blog] is:
 +
 
{{hc|~/.screenrc|<nowiki>
 
{{hc|~/.screenrc|<nowiki>
 
hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'
 
hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'
 
</nowiki>}}
 
</nowiki>}}
 +
 
=== Turn welcome message off ===
 
=== Turn welcome message off ===
 +
 
{{hc|~/.screenrc|
 
{{hc|~/.screenrc|
 
startup_message off
 
startup_message off
Line 135: Line 153:
  
 
=== Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title ===
 
=== Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title ===
 +
 
This one is pretty simple; just switch your current {{ic|hardstatus}} line into a {{ic|caption}} line with notification, and edit accordingly:
 
This one is pretty simple; just switch your current {{ic|hardstatus}} line into a {{ic|caption}} line with notification, and edit accordingly:
 
{{hc|~/.screenrc|<nowiki>
 
{{hc|~/.screenrc|<nowiki>
Line 147: Line 166:
  
 
===Use X scrolling mechanism===
 
===Use X scrolling mechanism===
 +
 
The scroll buffer of GNU Screen can be accessed with {{ic|ctrl+a}} {{ic|[}}. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line:
 
The scroll buffer of GNU Screen can be accessed with {{ic|ctrl+a}} {{ic|[}}. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line:
 
{{hc|~/.screenrc|
 
{{hc|~/.screenrc|
Line 153: Line 173:
  
 
=== Attach an existing running program to screen ===
 
=== Attach an existing running program to screen ===
 +
 
If you started a program outside screen, but now you would like it to be inside, you can use '''reptyr''' to reparent the process from it's current tty to one inside screen.
 
If you started a program outside screen, but now you would like it to be inside, you can use '''reptyr''' to reparent the process from it's current tty to one inside screen.
  
Line 161: Line 182:
  
 
===Add a GRUB entry to boot into Screen===
 
===Add a GRUB entry to boot into Screen===
 +
 
{{Deletion|[https://www.archlinux.org/news/end-of-initscripts-support/ Initscripts] is deprecated, and the use of runlevels as well. Also, this instructions are for legacy GRUB. If no one step up to update this section, propose to delete this section.}}
 
{{Deletion|[https://www.archlinux.org/news/end-of-initscripts-support/ Initscripts] is deprecated, and the use of runlevels as well. Also, this instructions are for legacy GRUB. If no one step up to update this section, propose to delete this section.}}
 
If you mostly use [[Xorg|X]] but occasionally want to run a Screen-as-window-manager session, here is one way to do it by adding a [[GRUB]] entry for Screen on a virtual console (text terminal).  
 
If you mostly use [[Xorg|X]] but occasionally want to run a Screen-as-window-manager session, here is one way to do it by adding a [[GRUB]] entry for Screen on a virtual console (text terminal).  
Line 202: Line 224:
  
 
===Setting a different bash prompt while in screen===
 
===Setting a different bash prompt while in screen===
 +
 
If you want a different bash prompt when in a screen session, add the following to your .bashrc:
 
If you want a different bash prompt when in a screen session, add the following to your .bashrc:
 
{{bc|<nowiki>
 
{{bc|<nowiki>
Line 214: Line 237:
  
 
===Turn off visual bell===
 
===Turn off visual bell===
 +
 
With this setting, screen will not make an ugly screen flash instead of a bell sound.
 
With this setting, screen will not make an ugly screen flash instead of a bell sound.
 
{{hc|~/.screenrc|
 
{{hc|~/.screenrc|
Line 220: Line 244:
  
 
== Troubleshooting ==
 
== Troubleshooting ==
 +
 
=== Fix Midnight Commander hard hang when starting in screen ===
 
=== Fix Midnight Commander hard hang when starting in screen ===
 +
 
In some cases (need deeper inspection) [https://bugzilla.redhat.com/show_bug.cgi?id=168076 old gpm bug] gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in {{ic|/etc/[[rc.conf]]}}.
 
In some cases (need deeper inspection) [https://bugzilla.redhat.com/show_bug.cgi?id=168076 old gpm bug] gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in {{ic|/etc/[[rc.conf]]}}.
  
 
=== Fix for residual editor text ===
 
=== Fix for residual editor text ===
 +
 
When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:
 
When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:
 
{{hc|~/.screenrc|
 
{{hc|~/.screenrc|
Line 229: Line 256:
 
}}
 
}}
  
== See Also ==
+
== See also ==
 +
 
 
* [http://www.macosxhints.com/article.php?story=20021114055617124 MacOSX Hints - Automatically using screen in your shell]
 
* [http://www.macosxhints.com/article.php?story=20021114055617124 MacOSX Hints - Automatically using screen in your shell]
 
* [http://wiki.gentoo.org/wiki/Screen Gentoo Wiki - Tutorial for screen]
 
* [http://wiki.gentoo.org/wiki/Screen Gentoo Wiki - Tutorial for screen]

Revision as of 18:58, 20 September 2014

zh-CN:GNU Screen

GNU Screen is a wrapper that allows separation between the text program and the shell from which it was launched. This allows the user to, for example, start a text program in a terminal in X, kill X, and continue to interact with the program.

Installation

GNU Screen can be installed using the screen package found in the official repositories.

Usage

Commands are entered pressing ctrl+a and then the key binding.

Common Commands

  • ctrl+a ? Displays commands and its defaults (VERY important)
  • ctrl+a : Enter to the command prompt of screen
  • ctrl+a " Window list
  • ctrl+a 0 opens window 0
  • ctrl+a A Rename the current window
  • ctrl+a a Sends ctrl+a to the current window
  • ctrl+a c Create a new window (with shell)
  • ctrl+a S Split current region into two regions
  • ctrl+a tab Switch the input focus to the next region
  • ctrl+a ctrl+a Toggle between current and previous region
  • ctrl+a Esc Enter Copy Mode (use enter to select a range of text)
  • ctrl+a ] Paste text
  • ctrl+a Q Close all regions but the current one
  • ctrl+a X Close the current region
  • ctrl+a d Detach from the current screen session, and leave it running. Use screen -r to resume

Command Prompt Commands

  • ctrl+a :quit Closes all windows and closes screen session
  • ctrl+a :source ~/.screenrc Reloads screenrc configuration file (can alternatively use /etc/screenrc)

Named sessions

To create a named session, run screen with the following command:

$ screen -S session_name

To prints a list of pid.tty.host strings identifying your screen sessions:

$ screen -list

To attach to a named screen session, run this command:

$ screen -x session_name

Autostart with systemd

This service autostarts screen for the specified user (e.g. systemctl enable screen@florian).

/etc/systemd/system/screen@.service
[Unit]
Description=screen
After=network.target

[Service]
Type=forking
User=%i
ExecStart=/usr/bin/screen -dmS autoscreen
ExecStop=/usr/bin/screen -S autoscreen -X quit

[Install]
WantedBy=multi-user.target

Tips and tricks

Change the escape key

The escape key can be changed with the escape option in ~/.screenrc. For example:

escape ``

sets the escape key to ` and

escape ^^^

sets it to ctrl+^.

Start at window 1

By default, the first screen window is 0. If you'd rather never have a window 0 and start instead with 1, add the following lines on your configuration:

~/.screenrc
bind c screen 1
bind ^c screen 1
bind 0 select 10                                                            
screen 1

Nested Screen Sessions

It is possible to get stuck in a nested screen session. A common scenario: you start an ssh session from within a screen session. Within the ssh session, you start screen. By default, the outer screen session that was launched first responds to ctrl+a commands. To send a command to the inner screen session, use ctrl+a a, followed by your command. For example:

  • ctrl+a a d Detaches the inner screen session.
  • ctrl+a a K Kills the inner screen session.

Use 256 colors

By default, screen uses an 8-color terminal emulator. To enable more colors, you need to be using a terminal that supports them and set the correct term value. This will use terminfo to describe how the ANSI escape codes will be interpreted. An entry in the terminfo database structure must exist, ncurses provides many common descriptions stored under /usr/share/terminfo/.

First try the generic value:

~/.screenrc
term screen-256color

If that does not work, try setting it based on the used terminal. When using xterm-based terminal:

~/.screenrc
term xterm-256color

When using rxvt-unicode:

~/.screenrc
term rxvt-unicode-256color
Note: /usr/share/terminfo/r/rxvt-unicode-256color is provided by rxvt-unicode-terminfo, which is installed as a dependency of rxvt-unicode. However, if you log into a server via SSH and run screen there, this terminfo file might not be available on the server. In this case it is recommended to copy /usr/share/terminfo/r/rxvt-unicode-256color on the server, it can be saved in ~/.terminfo.

As a last resort, try setting termcapinfo instead:

~/.screenrc
attrcolor b ".I"    # allow bold colors - necessary for some reason
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'   # tell screen how to set colors. AB = background, AF=foreground
defbce on    # use current bg color for erased chars

Informative statusbar

The default statusbar may be a little lacking. You may find this one more helpful:

~/.screenrc
hardstatus off
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'

Another possibility, taken from frodfrog's blog is:

~/.screenrc
hardstatus alwayslastline '%{= G}[ %{G}%H %{g}][%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}Load: %l %{g}][%{B}%Y-%m-%d %{W}%c:%s %{g}]'

Turn welcome message off

~/.screenrc
startup_message off

Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title

This one is pretty simple; just switch your current hardstatus line into a caption line with notification, and edit accordingly:

~/.screenrc
backtick 1 5 5 true
termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
hardstatus string "screen (%n: %t)"
caption string "%{= kw}%Y-%m-%d;%c %{= kw}%-Lw%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+Lw%1`"
caption always

This will give you something like screen (0 bash) in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection.

Use X scrolling mechanism

The scroll buffer of GNU Screen can be accessed with ctrl+a [. However, this is very inconvenient. To use the scroll bar of e.g. xterm or konsole, add the following line:

~/.screenrc
termcapinfo xterm* ti@:te@

Attach an existing running program to screen

If you started a program outside screen, but now you would like it to be inside, you can use reptyr to reparent the process from it's current tty to one inside screen.

Install the package reptyr from the official repositories.

Get the PID of the process (you can use ps ax for that). Now just enter the PID as argument to reptyr inside a screen window.

$ reptyr <pid>

Add a GRUB entry to boot into Screen

Tango-edit-cut.pngThis section is being considered for removal.Tango-edit-cut.png

Reason: Initscripts is deprecated, and the use of runlevels as well. Also, this instructions are for legacy GRUB. If no one step up to update this section, propose to delete this section. (Discuss in Talk:GNU Screen#)

If you mostly use X but occasionally want to run a Screen-as-window-manager session, here is one way to do it by adding a GRUB entry for Screen on a virtual console (text terminal).

GRUB allows you to designate what runlevel you want so we will use runlevel 4 for this purpose. Clone an appropriate GRUB entry and add a 4 to the kernel boot parameters list, like so:

# (0) Arch Linux
title  Arch Linux Screen
root   (hd0,2)
kernel /vmlinuz-linux root=/dev/disk/your_disk ro acpi_no_auto_ssdt irqpoll 4
initrd /initramfs-linux.img

Add some entries to /etc/inittab to indicate what should happen on runlevel 4, substituting your user name for <user>:

# GNU Screen on runlevel 4
scr2:4:respawn:/sbin/mingetty --autologin <user> tty1 linux

The above line uses mingettyAUR to automatically login some user to a virtual console on startup. The inittab line segments are separated by colons. The first part (scr*) is simply an id. The second part is the runlevel: This should only happen on runlevel 4 (which is not used in any default setup - 3 is by default for a tty login and 5 is for X). 'Respawn' causes init to repeat the command (i.e. autologin) if the user logs out. We will need to see that nothing else happens on virtual console 1 when we use runlevel 4, so remove 4 from the the first of the agetty lines:

c1:235:respawn:/sbin/agetty -8 38400 vc/1 linux

Once we are logged in, we want to ensure that screen is started. Add the following to the end of your ~/.bash_profile:

vico="$(tty | grep -oE ....$)"
case "$vico" in
   tty1) TERM=screen; exec /usr/bin/screen -R arch;;
esac

This checks for the current runlevel and will launch a screen session immediately after the automatic login if the runlevel is 4.

This can also be adapted to run screen on a virtual console next to X, simply checking for the current tty instead of the current runlevel. This checks to see if we are on virtual console 3:

vico="$(tty | grep -oE ....$)"
case "$vico" in
  vc/3) TERM=screen; exec /usr/bin/screen;;
esac

Set inittab/mingetty to automatically log in to vc/3 on runlevel 5 and you are set.

Setting a different bash prompt while in screen

If you want a different bash prompt when in a screen session, add the following to your .bashrc:

if [ -z $STY ]
then
        PS1="YOUR REGULAR PROMPT"
else  
        PS1="YOUR SCREEN PROMPT"
fi

[1]

Turn off visual bell

With this setting, screen will not make an ugly screen flash instead of a bell sound.

~/.screenrc
vbell off

Troubleshooting

Fix Midnight Commander hard hang when starting in screen

In some cases (need deeper inspection) old gpm bug gets alive. So, then you try to run mc inside screen, you get a frozen screen window. Try to kill gpm daemon before starting mc and/or disable it in /etc/rc.conf.

Fix for residual editor text

When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:

~/.screenrc
altscreen on

See also