Screen使用说明

最近在使用Python自动爬取dht网络中的磁力链接数据,爬虫启动之后网速会被占满。为了不影响日常使用,云服务器是最好的选择。使用ssh登录云服务器之后如果退出ssh所有操作会断开,如果需要保持操作的连续需要screen来帮助执行。

Screen

Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

在Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。

安装

1
2
3
apt install screen
yum install screen
yaourt install 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
40
41
42
43
44
45
46
47
48
49
50
51
52
$ screen -h
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]

screen [-AmRvx -ls -wipe][-d <作业名称>][-h <行数>][-r <作业名称>][-s ][-S <作业名称>]

Options:
-4 Resolve hostnames only to IPv4 addresses.
-6 Resolve hostnames only to IPv6 addresses.
-a Force all capabilities into each window\'s termcap.
-A -[r|R] Adapt all windows to the new display width & height.
-A  将所有的视窗都调整为目前终端机的大小。
-c file Read configuration file instead of \'.screenrc\'.
-d (-r) Detach the elsewhere running screen (and reattach here).
-d <作业名称>  将指定的screen作业离线。
-dmS name Start as daemon: Screen session in detached mode.
-D (-r) Detach and logout remote (and reattach here).
-D -RR Do whatever is needed to get a screen session.
-e xy Change command characters.
-f Flow control on, -fn = off, -fa = auto.
-h lines Set the size of the scrollback history buffer.
-h <行数>  指定视窗的缓冲区行数。
-i Interrupt output sooner when flow control is on.
-l Login mode on (update /var/run/utmp), -ln = off.
-ls [match] or
-list Do nothing, just list our SockDir [on possible matches].
-ls或--list  显示目前所有的screen作业。
-L Turn on output logging.
-m ignore $STY variable, do create a new screen session.
-m  即使目前已在作业中的screen作业,仍强制建立新的screen作业。
-O Choose optimal output rather than exact vt100 emulation.
-p window Preselect the named window if it exists.
-q Quiet startup. Exits with non-zero return code if unsuccessful.
-Q Commands will send the response to the stdout of the querying process.
-r [session] Reattach to a detached screen process.
-r <作业名称>  恢复离线的screen作业。
-R Reattach if possible, otherwise start a new session.
-R  先试图恢复离线的作业。若找不到离线的作业,即建立新的screen作业。
-s shell Shell to execute rather than $SHELL.
-s  指定建立新视窗时,所要执行的shell。
-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-S <作业名称>  指定screen作业的名称。
-t title Set title. (window\'s name).
-T term Use term as $TERM for windows, rather than "screen".
-U Tell screen to use UTF-8 encoding.
-v Print \"Screen version 4.01.00devel (GNU) 2-May-06\".
-v  显示版本信息。
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-wipe  检查目前所有的screen作业,并删除已经无法使用的screen作业。
-x Attach to a not detached screen. (Multi display mode).
-x  恢复之前离线的screen作业。
-X Execute <cmd> as a screen command in the specified session.

组合键

在每个screen session 下,所有命令都以 ctrl+a(C-a) 开始。
C-a ? -> 显示所有键绑定信息
C-a c -> 创建一个新的运行shell的窗口并切换到该窗口
C-a n -> Next,切换到下一个 window
C-a p -> Previous,切换到前一个 window
C-a 0…9 -> 切换到第 0…9 个 window
Ctrl+a [Space] -> 由视窗0循序切换到视窗9
C-a C-a -> 在两个最近使用的 window 间切换
C-a x -> 锁住当前的 window,需用用户密码解锁
C-a d -> detach,暂时离开当前session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。
C-a z -> 把当前session放到后台执行,用 shell 的 fg 命令则可回去。
C-a w -> 显示所有窗口列表
C-a t -> Time,显示当前时间,和系统的 load
C-a k -> kill window,强行关闭当前的 window
C-a [ -> 进入 copy mode,在 copy mode 下可以回滚、搜索、复制就像用使用 vi 一样
C-b Backward,PageUp
C-f Forward,PageDown
H(大写) High,将光标移至左上角
L Low,将光标移至左下角
0 移到行首
$ 行末
w forward one word,以字为单位往前移
b backward one word,以字为单位往后移
Space 第一次按为标记区起点,第二次按为终点
Esc 结束 copy mode
C-a ] -> Paste,把刚刚在 copy mode 选定的内容贴上

常用操作

创建新回话

1
screen -S name

强制创建会话

1
screen -dmS session_name

保留screen会话任务或程序

当需要临时离开时(会话中的程序不会关闭,仍在运行)可以用快捷键Ctrl+a d(即按住Ctrl,依次再按a,d)

列出所有会话

1
screen -ls

恢复screen会话

当回来时可以再执行执行:

1
screen -r dht

即可恢复到离开前创建的dht会话的工作界面。

如果忘记了,或者当时没有指定会话名,可以执行列出当前存在的会话列表,

暂时退出了dht会话,所以状态为Detached,当使用screen -r dht后状态就会变为Attached,11111是这个screen的会话的进程ID,恢复会话时也可以使用:

1
screen -r pid

退出会话

执行:exit ,会提示:[screen is terminating],表示已经成功退出screen会话。

或者:

1
screen -X -S [session] # you want to kill quit

screen分屏

Screen分屏,有的时候我们需要边观察一个命令的输出边执行一些操作,开两个CLI是一种解决方式,用screen分屏的功能也能做到。

首先在screen的session中按CTRL+a,X,这个时候就会出现第二个屏。

然后按CTRL+a,tab可以在两个屏幕之间自由切换。

切换到下个屏幕后,没有命令输入的提示符啊,怎么建立呢?

按CTRL+a c

不进入screen执行命令

还有一个命令要强烈推荐下,可以在detach的模式下跑screen的命令,我经常用这个命令去在后台持续抓包。就是在主tty中,运行

1
screen -dm cmd

如果我运行screen -dm tcpdump,然后再去查看screen的 session时,发现多了一个,但是我并没有进入screen的会话中,也就是并没有attach进去。


Screen使用说明
https://feater.top/linux/introduction-of-screen/
作者
JackeyLea
发布于
2020年6月16日
许可协议