GNU Screen

提供: ArchWiki
2015年2月13日 (金) 16:21時点におけるKusakata (トーク | 投稿記録)による版 (ページの作成:「Category:ターミナルエミュレータ en:GNU Screen ru:GNU Screen zh-CN:GNU Screen {{Related articles start}} {{Related|tmux}} {{Related articles end...」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

関連記事

GNU Screen はテキストプログラムとシェルを分離させることができるラッパーです。Screen を使うことで、例えば、X のターミナルでテキストプログラムを起動して、X を終了し、プログラムを使い続けることなどが可能になります。

インストール

GNU Screen は公式リポジトリscreen パッケージを使ってインストールできます。

使用方法

コマンドを入力するときは ctrl+a を押してからキーバインディングを押します。

よく使われるコマンド

  • ctrl+a ? コマンドとコマンドのデフォルトを表示 (とても重要です)
  • ctrl+a : screen のコマンドプロンプトに入る
  • ctrl+a " ウィンドウリスト
  • ctrl+a 0 ウィンドウ0を開く
  • ctrl+a A 現在のウィンドウの名前を変更
  • ctrl+a a 現在のウィンドウに ctrl+a を送信
  • ctrl+a c 新しいウィンドウ (とシェル) を作成
  • ctrl+a S 現在の領域を2つの領域に分割
  • ctrl+a tab 次の領域に入力フォーカスを移動
  • ctrl+a ctrl+a 現在の領域と前の領域を切り替え
  • ctrl+a Esc コピーモードに入ります (enter を使ってテキストの範囲を選択)
  • ctrl+a ] テキストの貼り付け
  • ctrl+a Q 現在の領域以外の全ての領域を閉じる
  • ctrl+a X 現在の領域を閉じる
  • ctrl+a d 現在のスクリーンセッションからデタッチ、動作し続ける。復帰するには screen -r を使う

コマンドプロンプトコマンド

  • ctrl+a :quit 全てのウィンドウと screen セッションを終了
  • ctrl+a :source ~/.screenrc screenrc 設定ファイルをリロード (/etc/screenrc を使うことも可能)

セッションに名前を付ける

名前付きセッションを作成するには、次のコマンドで screen を起動します:

$ screen -S session_name

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

$ screen -list

名前付きスクリーンセッションにアタッチするには、次のコマンドを実行:

$ screen -x session_name

systemd で自動起動

以下のサービスは特定のユーザーで screen を自動的に起動します (例: 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

エスケープキーを変更する

エスケープキーは ~/.screenrcescape オプションで変更できます。例:

escape ``

上の場合エスケープキーは ` に設定されます。

escape ^^^

これだと ctrl+^ に設定されます。

ウィンドウ1から起動

デフォルトでは、一番最初のスクリーンウィンドウは0です。ウィンドウ0を使わずに1から起動して欲しい場合、設定に以下の行を追加してください:

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

入れ子のスクリーンセッション

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
ノート: /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}]'

ウェルカムメッセージをオフにする

~/.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@

動作中のプログラムを 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.

公式リポジトリから reptyr パッケージをインストールしてください。

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>

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]

ビジュアルベルを切る

以下の設定を使えば、screen はベル音声の代わりに画面の点滅を使うのを止めます。

~/.screenrc
vbell off

トラブルシューティング

screen で起動した時に Midnight Commander がフリーズするのを修正

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.

エディタのテキストが残るのを修正

nano などのテキストエディタを開いて閉じると、テキストがターミナルに残ってしまうことがあります。これを修正するには、以下を記述してください:

~/.screenrc
altscreen on

参照