linux程序使用中断,使用Linux Screen 管理远程会话,防止连接断开导致的程序中断。...

Linux Screen

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

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

命令语法:

screen [-AmRvx -ls -wipe][-d ][-h ][-r ][-s ][-S ]

命令选项:-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 .sockname instead of ...

#当创建一个新的会话,可以使用此选项来指定会话一个有意义的名称。它可以代替默认的[ 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  as a screen command in the specified session.

Screen默认绑定键: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 listctrl+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会话

[root@localhost]# screen -ls

There is a screen on:

7745.stty1(Detached)

1 Socket in /var/run/screen/S-root.

[root@localhost]# screen -r stty1

清除dead 会话

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

会话共享

还有一种比较好玩的会话恢复,可以实现会话共享。假设你在和朋友在不同地点以相同用户登录一台机器,然后你创建一个screen会话,你朋友可以在他的终端上命令:

[root@localhost]#screen -x

这个命令会将你朋友的终端Attach到你的Screen会话上,并且你的终端不会被Detach。这样你就可以和朋友共享同一个会话了,如果你们当前又处于同一个窗口,那就相当于坐在同一个显示器前面,你的操作会同步演示给你朋友,你朋友的操作也会同步演示给你。当然,如果你们切换到这个会话的不同窗口中去,那还是可以分别进行不同的操作的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值