通过终端在远程服务器上执行长时间运行的任务时,因为突然的断开导致 SSH 会话终止,使得程序运行失败!使用 screen 能够避免上面情况的发生。同时,让我们在一个终端上可以运行多个终端程序,并可以自由切换这些终端。

Screen 或 GNU Screen 是一个终端多路复用器。换句话说,这意味着您可以启动一个屏幕会话,然后在该会话中打开任意数量的窗口(虚拟终端)。在 Screen 中运行的进程在它们的窗口不可见时将继续运行,即使您断开连接也是如此。

安装 screen

Debian or Ubuntu 操作系统安装方法如下:

1
2
sudo apt update
sudo apt install screen -y

RedHat or CentOS or Fedora 操作系统安装方法如下:

1
sudo yum install screen

使用 screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 创建一个名字为 main 的 screen 终端
screen -S main

# 查看打开了哪些 screen 终端
screen -ls

# 退出名字为 main 的 screen 终端,也可以不用名字,采用 screen -ls 罗列的 screen 进程 id
screen -d main
# 如果终端上正在运行程序,可以采用快捷键 "ctrl + a, d" 退出终端

# 强制分离打开main的screen并转移到当前窗口
screen -d -r main

# 重新进入名字为 main 的 screen 终端
screen -r main

# 共享会话。可以远程和朋友共享同一个会话,一方输入的命令事实在另一方可看
# 首先一方打开一个会话,如 main,然后,另一方输入如下命令
screen -x main

# 清理死掉的 screen
screen -wipe

# 退出或杀死 screen,使用 screen -ls 查看想要关闭的 screen id
screen -S screen_id -X quit
screen -S main -X quit # 或者使用 screen 名
# 或者进入 screen 后,正常退出终端
exit
ctrl + d

# 查看当前终端是哪个 screen
echo $STY

# 默认无法进行鼠标滚轮翻页,但可以复制和拷贝。如果想要翻页,可以进入翻页模式,快捷键是
ctrl + a, [
ctrl + a, esc
# 退出翻页模式,进入正常模式(可拷贝)
ctrl + c
esc

以下是一些用于管理 Linux Screen 窗口的最常用快捷键:

1
2
3
4
5
6
7
8
9
10
11
12
ctrl+a d      分离当前screen
ctrl+a k 关闭并杀死当前screen
Ctrl+a c Create a new window (with shell).
Ctrl+a " List all windows.
Ctrl+a 0 Switch to window 0 (by number).
Ctrl+a A Rename the current window.
Ctrl+a S Split current region horizontally into two regions.
Ctrl+a | Split current region vertically into two regions.
Ctrl+a tab Switch the input focus to the next region.
Ctrl+a Ctrl+a Toggle between the current and previous windows
Ctrl+a Q Close all regions but the current one.
Ctrl+a X Close the current region.

配置文件

1
vim ~/.screenrc

添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# $Id: screenrc,v 1.15 2003/10/08 11:39:03 zal Exp $
#
# /etc/screenrc
#
# This is the system wide screenrc.
#
# You can use this file to change the default behavior of screen system wide
# or copy it to ~/.screenrc and use it as a starting point for your own
# settings.
#
# Commands in this file are used to set options, bind screen functions to
# keys, redefine terminal capabilities, and to automatically establish one or
# more windows at the beginning of your screen session.
#
# This is not a comprehensive list of options, look at the screen manual for
# details on everything that you can put in this file.
#

# ------------------------------------------------------------------------------
# SCREEN SETTINGS
# ------------------------------------------------------------------------------

#startup_message off
#nethack on

#defflow on # will force screen to process ^S/^Q
deflogin on
#autodetach off

# turn visual bell on
vbell on
vbell_msg " Wuff ---- Wuff!! "

# define a bigger scrollback, default is 100 lines
# 鼠标翻页的行数,默认1024,可根据自己需要调整
defscrollback 10240000

# ------------------------------------------------------------------------------
# SCREEN KEYBINDINGS
# ------------------------------------------------------------------------------

# Remove some stupid / dangerous key bindings
bind ^k
#bind L
bind ^\
# Make them better
bind \\ quit
bind K kill
bind I login on
bind O login off
bind } history

# An example of a "screen scraper" which will launch urlview on the current
# screen window
#
#bind ^B eval "hardcopy_append off" "hardcopy -h $HOME/.screen-urlview" "screen urlview $HOME/.screen-urlview"

# ------------------------------------------------------------------------------
# TERMINAL SETTINGS
# ------------------------------------------------------------------------------

# The vt100 description does not mention "dl". *sigh*
termcapinfo vt100 dl=5\E[M

# turn sending of screen messages to hardstatus off
hardstatus off
# Set the hardstatus prop on gui terms to set the titlebar/icon title
termcapinfo xterm*|rxvt*|kterm*|Eterm* hs:ts=\E]0;:fs=\007:ds=\E]0;\007
# use this for the hard status string
hardstatus string "%h%? users: %u%?"

# 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 lastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"

# set these terminals up to be 'optimal' instead of vt100
termcapinfo xterm*|linux*|rxvt*|Eterm* OP

# Change the xterm initialization string from is2=\E[!p\E[?3;4l\E[4l\E>
# (This fixes the "Aborted because of window size change" konsole symptoms found
# in bug #134198)
termcapinfo xterm 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l'

# To get screen to add lines to xterm's scrollback buffer, uncomment the
# following termcapinfo line which tells xterm to use the normal screen buffer
# (which has scrollback), not the alternate screen buffer.
#
#termcapinfo xterm|xterms|xs|rxvt ti@:te@

# Enable non-blocking mode to better cope with flaky ssh connections.
defnonblock 5

# ------------------------------------------------------------------------------
# STARTUP SCREENS
# ------------------------------------------------------------------------------

# Example of automatically running some programs in windows on screen startup.
#
# The following will open top in the first window, an ssh session to monkey
# in the next window, and then open mutt and tail in windows 8 and 9
# respectively.
#
# screen top
# screen -t monkey ssh monkey
# screen -t mail 8 mutt
# screen -t daemon 9 tail -f /var/log/daemon.log


#hardstatus alwayslastline "%-Lw%50>%n%f* %t%+Lw%<"
hardstatus alwayslastline
shelltitle "$|bash"

hardstatus string '%{= kG}[ %Y/%m/%d %{G}%c:%s %{g}][%= %{= kw}%?%-Lw%?%{Y}%n*%f%t %{K}%?%+Lw%?%= %{kG}][ %{C}Author@Jinzhong Xu%{kg}]'
#hardstatus string '%{= kG}[ %Y/%m/%d %{G}%c:%s %{g}][%= %{= kw}%?%-Lw%?%{Y}%n*%f%t %{K}%?%+Lw%?%= %{kG}][ %{C}(F8)Screen (F9)Title (F10)Detach (F11)Prev (F12)Next %{kg}]'
#hardstatus string '%{= kG}[ %{G}%c:%s %{g}][%= %{= kw}%?%-Lw%?%{Y}%n*%f%t %{K}%?%+Lw%?%= %{kG}][ %{C}(F8)Screen (F9)Title (F10)Detach (F11)Prev (F12)Next %{kg}][%{kG} %l %Y/%m/%d %{kG}]'

# a short version, can use C-a
#hardstatus string '%{= kg}[ %{G}%c:%s %{g}][%= %{= kw}%?%-Lw%?%{Y}%n*%f%t %{K}%?%+Lw%?%= %{g}][%{kG} %D %Y/%m/%d %{g}]'

# Set default encoding using utf8
defutf8 on

## 解决中文乱码,这个要按需配置
defencoding utf8
encoding utf8 utf8

参考文献

  1. How To Use Linux Screen
  2. screen command in Linux with Examples
  3. Linux命令screen—终端切换,工作环境保存,画面同步,防断网