「GNU Screen」の版間の差分
(ページの作成:「Category:ターミナルエミュレータ en:GNU Screen ru:GNU Screen zh-CN:GNU Screen {{Related articles start}} {{Related|tmux}} {{Related articles end...」) |
(関連記事を同期) |
||
| (4人の利用者による、間の10版が非表示) | |||
| 1行目: | 1行目: | ||
| − | [[Category:ターミナル |
+ | [[Category:ターミナルマルチプレクサ]] |
| + | [[Category:GNU]] |
||
[[en:GNU Screen]] |
[[en:GNU Screen]] |
||
[[ru:GNU Screen]] |
[[ru:GNU Screen]] |
||
| − | [[zh- |
+ | [[zh-hans:GNU Screen]] |
{{Related articles start}} |
{{Related articles start}} |
||
{{Related|tmux}} |
{{Related|tmux}} |
||
| + | {{Related|Ratpoison}} |
||
| + | {{Related|Xpra}} |
||
{{Related articles end}} |
{{Related articles end}} |
||
| − | [ |
+ | [https://www.gnu.org/s/screen/ GNU Screen] はテキストプログラムとシェルを分離させることができるラッパーです。Screen を使うことで、例えば、X のターミナルでテキストプログラムを起動して、X を終了し、プログラムを使い続けることなどが可能になります。 |
== インストール == |
== インストール == |
||
| − | GNU Screen は |
+ | GNU Screen は {{pkg|screen}} パッケージを使って[[インストール]]できます。 |
== 使用方法 == |
== 使用方法 == |
||
| − | コマンドを入力するときは {{ic|ctrl+a}} を押してからキーバインディングを押します。 |
+ | コマンドを入力するときはエスケープキーの {{ic|ctrl+a}} を押してからキーバインディングを押します。 |
| + | |||
| + | ユーザーによってはデフォルトのエスケープキー {{ic|ctrl+a}} は不便かもしれません。[[#エスケープキーを変更する|下に書かれている]]ようにエスケープキーは別のキーに変更することが可能です。 |
||
=== よく使われるコマンド === |
=== よく使われるコマンド === |
||
| 25行目: | 30行目: | ||
* {{ic|ctrl+a}} {{ic|a}} 現在のウィンドウに {{ic|ctrl+a}} を送信 |
* {{ic|ctrl+a}} {{ic|a}} 現在のウィンドウに {{ic|ctrl+a}} を送信 |
||
* {{ic|ctrl+a}} {{ic|c}} 新しいウィンドウ (とシェル) を作成 |
* {{ic|ctrl+a}} {{ic|c}} 新しいウィンドウ (とシェル) を作成 |
||
| − | * {{ic|ctrl+a}} {{ic|S}} 現在の領域を2つの領域に分割 |
+ | * {{ic|ctrl+a}} {{ic|S}} 現在の領域を水平に2つの領域に分割 |
| + | * {{ic|ctrl+a}} {{ic|<nowiki>|</nowiki>}} 現在の領域を垂直に2つの領域に分割 |
||
* {{ic|ctrl+a}} {{ic|tab}} 次の領域に入力フォーカスを移動 |
* {{ic|ctrl+a}} {{ic|tab}} 次の領域に入力フォーカスを移動 |
||
* {{ic|ctrl+a}} {{ic|ctrl+a}} 現在の領域と前の領域を切り替え |
* {{ic|ctrl+a}} {{ic|ctrl+a}} 現在の領域と前の領域を切り替え |
||
| 32行目: | 38行目: | ||
* {{ic|ctrl+a}} {{ic|Q}} 現在の領域以外の全ての領域を閉じる |
* {{ic|ctrl+a}} {{ic|Q}} 現在の領域以外の全ての領域を閉じる |
||
* {{ic|ctrl+a}} {{ic|X}} 現在の領域を閉じる |
* {{ic|ctrl+a}} {{ic|X}} 現在の領域を閉じる |
||
| − | * {{ic|ctrl+a}} {{ic|d}} 現在の |
+ | * {{ic|ctrl+a}} {{ic|d}} 現在の screen セッションからデタッチ、動作し続ける。復帰するには {{ic|screen -r}} を使う |
=== コマンドプロンプトコマンド === |
=== コマンドプロンプトコマンド === |
||
| 45行目: | 51行目: | ||
$ screen -S ''session_name'' |
$ screen -S ''session_name'' |
||
| + | 既存のセッションの名前を変更するには、screen が実行しているときに次のコマンドを実行: {{ic|ctrl+a}} {{ic|:sessionname ''session_name''}}。 |
||
| − | To prints a list of ''pid.tty.host'' strings identifying your screen sessions: |
||
| + | |||
| + | screen のセッションを識別する ''pid.tty.host'' 文字列のリストを出力するには: |
||
$ screen -list |
$ screen -list |
||
| − | 名前付き |
+ | 名前付き screen セッションにアタッチするには、次のコマンドを実行: |
$ screen -x ''session_name'' |
$ screen -x ''session_name'' |
||
| + | |||
| + | または: |
||
| + | |||
| + | $ screen -r ''session_name'' |
||
| + | |||
| + | === Screen のカスタマイズ === |
||
| + | |||
| + | Screen のデフォルト設定を変更したい場合は {{ic|.screenrc}} ファイル (例: {{ic|~/.screenrc}}) を使用するか (起動時に実行するコマンドを記述)、あるいはコロン ({{ic|-}}) コマンドを使います。 |
||
| + | |||
| + | == ヒントとテクニック == |
||
=== systemd で自動起動 === |
=== systemd で自動起動 === |
||
| − | 以下のサービスは特定のユーザーで screen を自動的に起動します (例: {{ic|systemctl enable screen@florian}}) |
+ | 以下のサービスは特定のユーザーで screen を自動的に起動します (例: {{ic|systemctl enable screen@florian}})。[[systemd/ユーザー|systemd --user]] インスタンスは特定のユーザーの最後のセッションが閉じたときに終了されることが保証されていないので、システムユニットとして実行する必要があります。 |
{{hc|/etc/systemd/system/screen@.service|<nowiki> |
{{hc|/etc/systemd/system/screen@.service|<nowiki> |
||
| 63行目: | 81行目: | ||
[Service] |
[Service] |
||
| − | Type= |
+ | Type=simple |
User=%i |
User=%i |
||
| − | ExecStart=/usr/bin/screen - |
+ | ExecStart=/usr/bin/screen -DmS autoscreen |
ExecStop=/usr/bin/screen -S autoscreen -X quit |
ExecStop=/usr/bin/screen -S autoscreen -X quit |
||
| 71行目: | 89行目: | ||
WantedBy=multi-user.target |
WantedBy=multi-user.target |
||
</nowiki>}} |
</nowiki>}} |
||
| − | |||
| − | == Tips and tricks == |
||
=== エスケープキーを変更する === |
=== エスケープキーを変更する === |
||
| + | デフォルトのエスケープキーを変更する理由としては、"a" を押すのが左手の小指だから、あるいは、[[readline|GNU Readline]] や [[Bash]] などのシェルでは {{ic|ctrl+a}} が {{ic|beginning-of-line}} コマンドにマッピングされていることが挙げられます。 |
||
| − | エスケープキーは {{ic|~/.screenrc}} の {{ic|escape}} オプションで変更できます。例: |
||
| + | |||
| − | {{bc|escape ``}} |
||
| − | + | エスケープキーは {{ic|~/.screenrc}} の {{ic|escape}} オプションで変更できます。あるいは {{ic|screen}} を実行するときに {{ic|-e}} オプションを指定します。 |
|
| + | |||
| − | {{bc|escape ^^^}} |
||
| + | 例えば、シェルやエディタで {{ic|ctrl+j}} を使用することがないのであれば、{{ic|escape ^Jj}} でエスケープキーを {{ic|ctrl-j}} に設定できます。2番目の "j" は {{ic|ctrl-j j}} でターミナルに {{ic|ctrl-j}} リテラルを送信することを意味します。[[Dvorak]] キーボードを使っている場合、{{ic|ctrl-t}} ({{ic|escape ^Tt}}) が便利だと思います。 |
||
| − | これだと {{ic|ctrl+^}} に設定されます。 |
||
| + | |||
| + | 他にも、エスケープキーを {{ic|`}} に設定する {{ic|escape ``}} オプションや、{{ic|ctrl+^}} に設定する {{ic|escape ^^^}} などが存在します。 |
||
| + | |||
| + | Screen のドキュメントではエスケープキーのことを「コマンド文字」と呼ぶこともあります。 |
||
=== ウィンドウ1から起動 === |
=== ウィンドウ1から起動 === |
||
| 93行目: | 113行目: | ||
}} |
}} |
||
| − | === 入れ子の |
+ | === 入れ子の screen セッション === |
| + | screen セッションをネストすると固まってしまうことがあります。screen セッションの中で ssh セッションを開始して、さらに ssh セッションの中で、screen を起動した場合を考えてください。デフォルトでは、{{ic|ctrl+a}} コマンドに反応するのは最初に起動した (外側の) screen セッションだけです。内側の screen セッションにコマンドを送信するには、{{ic|ctrl+a}} {{ic|a}} と入力してからコマンドを押してください。例: |
||
| − | 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}} |
+ | * {{ic|ctrl+a}} {{ic|a}} {{ic|d}} 内側の screen セッションをデタッチ。 |
| − | * {{ic|ctrl+a}} {{ic|a}} {{ic|K}} |
+ | * {{ic|ctrl+a}} {{ic|a}} {{ic|K}} 内側の screen セッションを終了。 |
| − | === |
+ | === 毎回シェルで Screen を起動する === |
| + | Bash と Zsh の場合、エイリアスの前に {{ic|.bashrc}} または {{ic|.zshrc}} に以下のスニペットを追加してください: |
||
| + | {{hc|~/.bashrc or ~/.zshrc|<nowiki> |
||
| − | 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 [http://aperiodic.net/screen/commands:term term] value. This will use [[wikipedia:Terminfo|terminfo]] to describe how the [[wikipedia:ANSI escape code|ANSI escape codes]] will be interpreted. An entry in the terminfo database structure must exist, {{Pkg|ncurses}} provides many common descriptions stored under {{ic|/usr/share/terminfo/}}. |
||
| + | if [[ -z "$STY" ]]; then |
||
| + | screen -xRR session_name |
||
| + | fi |
||
| + | </nowiki>}} |
||
| + | |||
| + | === 256色を使う === |
||
| + | |||
| + | デフォルトでは、screen は8色のターミナルエミュレータを使います。他の色を使えるようにするには、多色対応しているターミナルを使用して [http://aperiodic.net/screen/commands:term term] の値を適切に設定する必要があります。[[wikipedia:Terminfo|terminfo]] を使って[[wikipedia:ANSI escape code|ANSI エスケープコード]]の解釈方法が決められます。terminfo のデータベースにエントリが存在している必要があり、{{Pkg|ncurses}} に一般的な記述子が含まれていて {{ic|/usr/share/terminfo/}} に保存されます。 |
||
| + | まずは汎用の値を試してください: |
||
| − | First try the generic value: |
||
{{hc|~/.screenrc| |
{{hc|~/.screenrc| |
||
| 109行目: | 138行目: | ||
}} |
}} |
||
| + | 上記で上手く行かない場合、使用しているターミナルにあわせて設定してみてください。例えば [[xterm]] ベースのターミナルを使っている場合: |
||
| − | If that does not work, try setting it based on the used terminal. When using [[xterm]]-based terminal: |
||
{{hc|~/.screenrc| |
{{hc|~/.screenrc| |
||
| 115行目: | 144行目: | ||
}} |
}} |
||
| − | + | [[rxvt-unicode]] を使用する場合: |
|
{{hc|~/.screenrc| |
{{hc|~/.screenrc| |
||
| 121行目: | 150行目: | ||
}} |
}} |
||
| − | {{Note|{{ic|/usr/share/terminfo/r/rxvt-unicode-256color}} |
+ | {{Note|{{ic|/usr/share/terminfo/r/rxvt-unicode-256color}} は {{Pkg|rxvt-unicode-terminfo}} に入っています。このパッケージは {{Pkg|rxvt-unicode}} の依存パッケージとしてインストールされます。ただし、[[SSH]] を介してサーバーにログインして ''screen'' を実行している場合、terminfo ファイルがサーバー側に存在しない可能性があります。その場合、サーバーに {{ic|/usr/share/terminfo/r/rxvt-unicode-256color}} をコピーして {{ic|~/.terminfo}} に保存することを推奨します。}} |
| − | + | 上記の設定が駄目ならば最後の手段として、[http://aperiodic.net/screen/commands:termcapinfo termcapinfo] を設定してください: |
|
{{hc|~/.screenrc|<nowiki> |
{{hc|~/.screenrc|<nowiki> |
||
| 131行目: | 160行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| − | === |
+ | === 有益なステータスバー === |
| + | デフォルトのステータスバーは情報が少なすぎます。以下のように設定すると良いでしょう: |
||
| − | The default statusbar may be a little lacking. You may find this one more helpful: |
||
{{hc|~/.screenrc|<nowiki> |
{{hc|~/.screenrc|<nowiki> |
||
| 141行目: | 170行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| − | + | あるいは、[http://www.fordfrog.com/2012/09/02/71/ frodfrog のブログ] に書かれている例: |
|
{{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>}} |
||
| + | |||
| + | ステータスバーを上に表示するには: |
||
| + | |||
| + | {{hc|~/.screenrc|<nowiki> |
||
| + | hardstatus firstline |
||
</nowiki>}} |
</nowiki>}} |
||
| 153行目: | 188行目: | ||
}} |
}} |
||
| − | === |
+ | === hardstatus 行を urxvt|xterm|aterm のウィンドウタイトルに動的に変える === |
| + | {{ic|hardstatus}} 行を通知を表示する {{ic|caption}} 行に切り替えて、以下のように編集するだけです: |
||
| − | 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> |
||
backtick 1 5 5 true |
backtick 1 5 5 true |
||
| 164行目: | 199行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| + | ターミナルエミュレータのタイトルに {{ic|screen (0 bash)}} と表示されるようになります。caption は日付と時刻を表示して、screen ウィンドウをカラー化します。 |
||
| − | This will give you something like {{ic|screen (0 bash)}} in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection. |
||
| + | ===X のスクロール機能を使う=== |
||
| − | ===Use X scrolling mechanism=== |
||
| + | GNU Screen のスクロールバッファは {{ic|ctrl+a}} {{ic|[}} で読むことができますが、不便です。xterm や konsole などのスクロールバーを使うには、以下の行を追加してください: |
||
| − | 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| |
||
termcapinfo xterm* ti@:te@ |
termcapinfo xterm* ti@:te@ |
||
| 175行目: | 210行目: | ||
=== 動作中のプログラムを screen にアタッチ === |
=== 動作中のプログラムを screen にアタッチ === |
||
| + | screen の外で起動したプログラムを中に持って行きたい場合、'''reptyr''' を使うことでプロセスの親を tty から 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. |
||
| − | + | {{Pkg|reptyr}} パッケージを[[インストール]]してください。 |
|
| + | プロセスの PID を取得してください ({{ic|ps ax}} を使えば取得できます)。reptyr の引数として取得した PID を入力して screen ウィンドウの中で実行してください: |
||
| − | Get the PID of the process (you can use {{ic|ps ax}} for that). Now just enter the PID as argument to reptyr inside a screen window. |
||
{{bc|$ reptyr ''<pid>''}} |
{{bc|$ reptyr ''<pid>''}} |
||
| − | === |
+ | ===screen で別の bash プロンプトを設定=== |
| + | screen セッションで別の bash プロンプトを使いたい場合、以下を {{ic|.bashrc}} に追加してください [http://serverfault.com/questions/257975/how-to-check-if-im-in-screen-session]: |
||
| − | If you want a different bash prompt when in a screen session, add the following to your .bashrc: |
||
{{bc|<nowiki> |
{{bc|<nowiki> |
||
if [ -z $STY ] |
if [ -z $STY ] |
||
| 193行目: | 228行目: | ||
fi |
fi |
||
</nowiki>}} |
</nowiki>}} |
||
| − | [http://serverfault.com/questions/257975/how-to-check-if-im-in-screen-session] |
||
===ビジュアルベルを切る=== |
===ビジュアルベルを切る=== |
||
| 202行目: | 236行目: | ||
}} |
}} |
||
| + | === 垂直・水平バーを表示しない === |
||
| − | == トラブルシューティング == |
||
| + | 垂直バーを消すには: |
||
| + | {{hc| |
||
| + | head=$ ~/.screenrc |output=rendition so =00 |
||
| + | }} |
||
| + | 背景・前景色をデフォルト (d) に設定して何も表示しない (" ") ようにします。水平バーを消すには: |
||
| − | === screen で起動した時に Midnight Commander がフリーズするのを修正 === |
||
| + | {{hc|~/.screenrc| |
||
| + | caption string "%{03} " |
||
| + | }} |
||
| + | == トラブルシューティング == |
||
| − | 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]]}}. |
||
=== エディタのテキストが残るのを修正 === |
=== エディタのテキストが残るのを修正 === |
||
| 214行目: | 255行目: | ||
altscreen on |
altscreen on |
||
}} |
}} |
||
| + | |||
| + | === ウィンドウリストの Name カラムに "bash" としか表示されない === |
||
| + | |||
| + | {{ic|~/.screenrc}} に以下を追加してください: |
||
| + | {{hc|head=~/.screenrc|output=windowlist string "%4n %h%=%f"}} |
||
== 参照 == |
== 参照 == |
||
| + | * [https://www.gnu.org/software/screen/manual/screen.html GNU Screen ユーザーマニュアル] |
||
| + | * [https://wiki.gentoo.org/wiki/Screen Gentoo Wiki - screen のチュートリアル] |
||
| + | * [https://bbs.archlinux.org/viewtopic.php?id=55618 Arch フォーラム - .screenrc 設定とスクリーンショット] |
||
| + | * [https://bbs.archlinux.org/viewtopic.php?id=50647 Arch フォーラム - Regarding 256 color issue with urxvt] |
||
* [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 - screen のチュートリアル] |
||
| − | * [https://bbs.archlinux.org/viewtopic.php?id=50647 Arch Forums - Regarding 256 color issue with urxvt] |
||
| − | * [https://bbs.archlinux.org/viewtopic.php?id=55618 Arch Forums - .screenrc configs with screenshots] |
||
* [[Ratpoison]] - gnu screen ベースのウィンドウマネージャ |
* [[Ratpoison]] - gnu screen ベースのウィンドウマネージャ |
||
| + | * [[Xpra]] - コマンドラインプログラムを screen がデタッチ・再アタッチするのと同じように、X プログラムをデタッチ・再アタッチできるユーティリティ |
||
| − | * [[en2:Xpra|Xpra]] - An utility to detach/reattach X programs, in a way similar as screen does for command line based programs |
||
2024年4月22日 (月) 23:50時点における最新版
GNU Screen はテキストプログラムとシェルを分離させることができるラッパーです。Screen を使うことで、例えば、X のターミナルでテキストプログラムを起動して、X を終了し、プログラムを使い続けることなどが可能になります。
目次
- 1 インストール
- 2 使用方法
- 3 ヒントとテクニック
- 3.1 systemd で自動起動
- 3.2 エスケープキーを変更する
- 3.3 ウィンドウ1から起動
- 3.4 入れ子の screen セッション
- 3.5 毎回シェルで Screen を起動する
- 3.6 256色を使う
- 3.7 有益なステータスバー
- 3.8 ウェルカムメッセージをオフにする
- 3.9 hardstatus 行を urxvt|xterm|aterm のウィンドウタイトルに動的に変える
- 3.10 X のスクロール機能を使う
- 3.11 動作中のプログラムを screen にアタッチ
- 3.12 screen で別の bash プロンプトを設定
- 3.13 ビジュアルベルを切る
- 3.14 垂直・水平バーを表示しない
- 4 トラブルシューティング
- 5 参照
インストール
GNU Screen は screen パッケージを使ってインストールできます。
使用方法
コマンドを入力するときはエスケープキーの ctrl+a を押してからキーバインディングを押します。
ユーザーによってはデフォルトのエスケープキー ctrl+a は不便かもしれません。下に書かれているようにエスケープキーは別のキーに変更することが可能です。
よく使われるコマンド
ctrl+a?コマンドとコマンドのデフォルトを表示 (とても重要です)ctrl+a:screen のコマンドプロンプトに入るctrl+a"ウィンドウリストctrl+a0ウィンドウ0を開くctrl+aA現在のウィンドウの名前を変更ctrl+aa現在のウィンドウにctrl+aを送信ctrl+ac新しいウィンドウ (とシェル) を作成ctrl+aS現在の領域を水平に2つの領域に分割ctrl+a|現在の領域を垂直に2つの領域に分割ctrl+atab次の領域に入力フォーカスを移動ctrl+actrl+a現在の領域と前の領域を切り替えctrl+aEscコピーモードに入ります (enter を使ってテキストの範囲を選択)ctrl+a]テキストの貼り付けctrl+aQ現在の領域以外の全ての領域を閉じるctrl+aX現在の領域を閉じるctrl+ad現在の screen セッションからデタッチ、動作し続ける。復帰するにはscreen -rを使う
コマンドプロンプトコマンド
ctrl+a:quit全てのウィンドウと screen セッションを終了ctrl+a:source ~/.screenrcscreenrc 設定ファイルをリロード (/etc/screenrc を使うことも可能)
セッションに名前を付ける
名前付きセッションを作成するには、次のコマンドで screen を起動します:
$ screen -S session_name
既存のセッションの名前を変更するには、screen が実行しているときに次のコマンドを実行: ctrl+a :sessionname session_name。
screen のセッションを識別する pid.tty.host 文字列のリストを出力するには:
$ screen -list
名前付き screen セッションにアタッチするには、次のコマンドを実行:
$ screen -x session_name
または:
$ screen -r session_name
Screen のカスタマイズ
Screen のデフォルト設定を変更したい場合は .screenrc ファイル (例: ~/.screenrc) を使用するか (起動時に実行するコマンドを記述)、あるいはコロン (-) コマンドを使います。
ヒントとテクニック
systemd で自動起動
以下のサービスは特定のユーザーで screen を自動的に起動します (例: systemctl enable screen@florian)。systemd --user インスタンスは特定のユーザーの最後のセッションが閉じたときに終了されることが保証されていないので、システムユニットとして実行する必要があります。
/etc/systemd/system/screen@.service
[Unit] Description=screen After=network.target [Service] Type=simple User=%i ExecStart=/usr/bin/screen -DmS autoscreen ExecStop=/usr/bin/screen -S autoscreen -X quit [Install] WantedBy=multi-user.target
エスケープキーを変更する
デフォルトのエスケープキーを変更する理由としては、"a" を押すのが左手の小指だから、あるいは、GNU Readline や Bash などのシェルでは ctrl+a が beginning-of-line コマンドにマッピングされていることが挙げられます。
エスケープキーは ~/.screenrc の escape オプションで変更できます。あるいは screen を実行するときに -e オプションを指定します。
例えば、シェルやエディタで ctrl+j を使用することがないのであれば、escape ^Jj でエスケープキーを ctrl-j に設定できます。2番目の "j" は ctrl-j j でターミナルに ctrl-j リテラルを送信することを意味します。Dvorak キーボードを使っている場合、ctrl-t (escape ^Tt) が便利だと思います。
他にも、エスケープキーを ` に設定する escape `` オプションや、ctrl+^ に設定する escape ^^^ などが存在します。
Screen のドキュメントではエスケープキーのことを「コマンド文字」と呼ぶこともあります。
ウィンドウ1から起動
デフォルトでは、一番最初のスクリーンウィンドウは0です。ウィンドウ0を使わずに1から起動して欲しい場合、設定に以下の行を追加してください:
~/.screenrc
bind c screen 1 bind ^c screen 1 bind 0 select 10 screen 1
入れ子の screen セッション
screen セッションをネストすると固まってしまうことがあります。screen セッションの中で ssh セッションを開始して、さらに ssh セッションの中で、screen を起動した場合を考えてください。デフォルトでは、ctrl+a コマンドに反応するのは最初に起動した (外側の) screen セッションだけです。内側の screen セッションにコマンドを送信するには、ctrl+a a と入力してからコマンドを押してください。例:
ctrl+aad内側の screen セッションをデタッチ。ctrl+aaK内側の screen セッションを終了。
毎回シェルで Screen を起動する
Bash と Zsh の場合、エイリアスの前に .bashrc または .zshrc に以下のスニペットを追加してください:
~/.bashrc or ~/.zshrc
if [[ -z "$STY" ]]; then screen -xRR session_name fi
256色を使う
デフォルトでは、screen は8色のターミナルエミュレータを使います。他の色を使えるようにするには、多色対応しているターミナルを使用して term の値を適切に設定する必要があります。terminfo を使ってANSI エスケープコードの解釈方法が決められます。terminfo のデータベースにエントリが存在している必要があり、ncurses に一般的な記述子が含まれていて /usr/share/terminfo/ に保存されます。
まずは汎用の値を試してください:
~/.screenrc
term screen-256color
上記で上手く行かない場合、使用しているターミナルにあわせて設定してみてください。例えば xterm ベースのターミナルを使っている場合:
~/.screenrc
term xterm-256color
rxvt-unicode を使用する場合:
~/.screenrc
term rxvt-unicode-256color
上記の設定が駄目ならば最後の手段として、termcapinfo を設定してください:
~/.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
有益なステータスバー
デフォルトのステータスバーは情報が少なすぎます。以下のように設定すると良いでしょう:
~/.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}]'
あるいは、frodfrog のブログ に書かれている例:
~/.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
hardstatus firstline
ウェルカムメッセージをオフにする
~/.screenrc
startup_message off
hardstatus 行を urxvt|xterm|aterm のウィンドウタイトルに動的に変える
hardstatus 行を通知を表示する caption 行に切り替えて、以下のように編集するだけです:
~/.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
ターミナルエミュレータのタイトルに screen (0 bash) と表示されるようになります。caption は日付と時刻を表示して、screen ウィンドウをカラー化します。
X のスクロール機能を使う
GNU Screen のスクロールバッファは ctrl+a [ で読むことができますが、不便です。xterm や konsole などのスクロールバーを使うには、以下の行を追加してください:
~/.screenrc
termcapinfo xterm* ti@:te@
動作中のプログラムを screen にアタッチ
screen の外で起動したプログラムを中に持って行きたい場合、reptyr を使うことでプロセスの親を tty から screen に変えることができます。
プロセスの PID を取得してください (ps ax を使えば取得できます)。reptyr の引数として取得した PID を入力して screen ウィンドウの中で実行してください:
$ reptyr <pid>
screen で別の bash プロンプトを設定
screen セッションで別の bash プロンプトを使いたい場合、以下を .bashrc に追加してください [1]:
if [ -z $STY ]
then
PS1="YOUR REGULAR PROMPT"
else
PS1="YOUR SCREEN PROMPT"
fi
ビジュアルベルを切る
以下の設定を使えば、screen はベル音声の代わりに画面の点滅を使うのを止めます。
~/.screenrc
vbell off
垂直・水平バーを表示しない
垂直バーを消すには:
$ ~/.screenrc
rendition so =00
背景・前景色をデフォルト (d) に設定して何も表示しない (" ") ようにします。水平バーを消すには:
~/.screenrc
caption string "%{03} "
トラブルシューティング
エディタのテキストが残るのを修正
nano などのテキストエディタを開いて閉じると、テキストがターミナルに残ってしまうことがあります。これを修正するには、以下を記述してください:
~/.screenrc
altscreen on
ウィンドウリストの Name カラムに "bash" としか表示されない
~/.screenrc に以下を追加してください:
~/.screenrc
windowlist string "%4n %h%=%f"
参照
- GNU Screen ユーザーマニュアル
- Gentoo Wiki - screen のチュートリアル
- Arch フォーラム - .screenrc 設定とスクリーンショット
- Arch フォーラム - Regarding 256 color issue with urxvt
- MacOSX Hints - Automatically using screen in your shell
- Ratpoison - gnu screen ベースのウィンドウマネージャ
- Xpra - コマンドラインプログラムを screen がデタッチ・再アタッチするのと同じように、X プログラムをデタッチ・再アタッチできるユーティリティ