设为首页 收藏本站
查看: 605|回复: 0

[经验分享] 使用Linux Screen 管理远程会话,防止连接断开导致的程序中断

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-25 13:28:26 | 显示全部楼层 |阅读模式
Linux Screen

系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份、ftp 传输等等。通常情况下我们都是为每一个这样的任务开一个远程终端窗口,因为它们执行的时间太长了。必须等待它们执行完毕,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了。

Screen  是一个全屏窗口管理器,复用多个进程(通常是交互式shell)之间的物理终端。

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

命令选项:
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
-4            Resolve hostnames only to IPv4 addresses.#只对IPv4地址解析主机名。

-6            Resolve hostnames only to IPv6 addresses.

-a            Force all capabilities into each window\'s termcap. #每个窗口的termcap强制使用所有功能。

-A -[r|R]     Adapt all windows to the new display width & height. #所有窗口适应新的显示宽度和高度。

-c file       Read configuration file instead of '.screenrc'. #读取配置文件代替“.screenrc”。

-d (-r)       Detach the elsewhere running screen (and reattach here). #分离其它处运行的Screen(并在此处重新会话)

-dmS name     Start as daemon: Screen session in detached mode.#在分离模式下启动守护进程:Screen会话

-D (-r)       Detach and logout remote (and reattach here). #分离并退出远程(并在此处重新会话)。

-D -RR        Do whatever is needed to get a screen session.#做任何需要得到一个Screen会话。

-e xy         Change command characters.

-f            Flow control on, -fn = off, -fa = auto.#流控制开关

-h lines      Set the size of the scrollback history buffer.#设置回滚历史缓存的大小。

-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].#什么都不做,仅列出所有的SockDir

-L            Turn on output logging.#打开输出日志。

-m            ignore $STY variable, do create a new screen session.#忽略$STY变量,创建一个新的Screen会话。

-O            Choose optimal output rather than exact vt100 emulation.#选择最优的输出而不是确切vt100仿真。

-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.#命令将响应发送到stdout的查询过程。

-r [session]  Reattach to a detached screen process.#重新连接一个断开的Screen进程。

-R            Reattach if possible, otherwise start a new session.#重新连接Screen,如果成功,其他选项将被忽略,否则开始一个新的会话。

-s shell      Shell to execute rather than $SHELL.#Shell执行,而不是环境变量$SHELL指定的程序。

-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
#当创建一个新的会话,可以使用此选项来指定会话一个有意义的名称。它可以代替默认的[ tty.host ]后缀。

-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.#告知Screen使用UTF-8编码。

-v            Print "Screen version 4.01.00devel (GNU) 2-May-06". #打印版本。

-wipe [match] Do nothing, just clean up SockDir [on possible matches].

-x            Attach to a not detached screen. (Multi display mode).#附加到一个不独立的Screen。(多显示模式)。

-X            Execute <cmd> as a screen command in the specified session.




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
ctrl+a ? #Displays commands and its defaults (VERY important)

ctrl+a : #Enter to the command prompt of screen

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

ctrl+a " #Window list



1
2
3
ctrl+a :quit #Closes all windows and closes screen session

ctrl+a :source ~/.screenrc #Reloads screenrc configuration file (can alternatively use /etc/screenrc)





安装Screen:
yum install screen

创建一个新的窗口
screen -S stty1  (stty1为自己起的会话名)

screen启动后,会创建第一个窗口,也就是窗口No. 0,并在其中打开一个系统默认的shell,一般都会是bash。所以你敲入命令screen之后,会立刻又返回到命令提示符,仿佛什么也没有发生似的,其实你已经进入Screen的世界了。当然,也可以在screen命令之后加入你喜欢的参数,使之直接打开你指定的程序,例如:
screen vi test.txt
screen创建一个执行vi test.txt的单窗口会话,退出vi 将退出该窗口/会话。

会话分离与恢复
你可以不中断screen窗口中程序的运行而暂时断开(detach)screen会话,并在随后时间重新连接(attach)该会话,重新控制各窗口中运行的程序。
那么在screen窗口键入C-a d
会给出提示:[detached from 7745.stty1]

找到该screen会话
[iyunv@localhost]# screen -ls
There is a screen on:
7745.stty1(Detached)
1 Socket in /var/run/screen/S-root.
[iyunv@localhost]# screen -r stty1

清除dead 会话
如果由于某种原因其中一个会话死掉了(例如人为杀掉该会话),这时screen -list会显示该会话为dead状态。使用screen -wipe命令清除该会话。

会话共享
还有一种比较好玩的会话恢复,可以实现会话共享。假设你在和朋友在不同地点以相同用户登录一台机器,然后你创建一个screen会话,你朋友可以在他的终端上命令:
[iyunv@localhost]#screen -x
这个命令会将你朋友的终端Attach到你的Screen会话上,并且你的终端不会被Detach。这样你就可以和朋友共享同一个会话了,如果你们当前又处于同一个窗口,那就相当于坐在同一个显示器前面,你的操作会同步演示给你朋友,你朋友的操作也会同步演示给你。当然,如果你们切换到这个会话的不同窗口中去,那还是可以分别进行不同的操作的。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-25405-1-1.html 上篇帖子: Django构建自动化运维资产管理系统 下篇帖子: 在Linux中创建带有前缀0的数值序列的多种方法 Linux 程序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表