终端会话控制工具-screen
2013-01-08 10:32:56 阿炯

Screen被称之为一个全屏窗口管理器,用他可以轻松在一个物理终端上获得多个虚拟终端的效果。它可以在多个进程之间多路复用一个物理终端的窗口管理器,这意味着你能够使用一个单一的终端窗口运行多终端的应用。Screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的Telnet/SSH连接窗口那样。

有时候我们需要保存整个终端会话的状况,可以使用screen命令,通过man命令来查看screen提供的功能非常多,但是平时工作用到这些就足够了。简单来说,Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器。Screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的telnet/SSH连接窗口那样。

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Each virtual terminal provides the functions of the DEC VT100 terminal and, in addition, several control functions from the ANSI X3.64 (ISO 6429) and ISO 2022 standards (e.g., insert/delete line and support for multiple character sets).

There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows the user to move text regions between windows. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, turn output logging on and off, copy text between windows, view the scrollback history, switch between windows, etc. All windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.

使用示例简介:

首先ssh登录到远程服务器:
#ssh user@host

然后就可以使用screen命令来开启一个终端会话,直接输入screen命令就可以了:
#screen

另外也可以在screen命令后面加shell命令,会开启一个新的screen会话并且执行命令:
#screen vim /etc/hosts

以上命令会直接开启一个新的会话并且执行。然后就可以跟平时一样在终端下进行工作,工作完毕后通过exit命令退出会话。 接下来的重点是会话的保存,比如我文件没有编辑完,这时候我有别的事情需要离开,那么就可以保存此时的会话:按ctrl + a, 然后再按d键,会话就被保存了起来:
[detached from 8522.pts-3.pdebian]

事情处理完了,需要把会话重新调出来,怎么办?通过screen -ls命令可以将保存的会话全部列出:
# screen -ls
There are screens on:
        8522.pts-3.pdebian      (2013年01月08日 10时11分30秒)   (Detached)
        8485.pts-2.pdebian      (2013年01月08日 10时01分50秒)   (Attached)
2 Sockets in /var/run/screen/S-root

要进入哪一个会话,比如我们刚才的会话是8522,那么就可以使用:
#screen -r 8522

进入这个会话重新工作了。

screen的命令参数和指令有很多,可以更深入的学习。

最新版本:4.4
新版改进了布局、窗口组、鼠标支持、垂直分割、增加了一系列新的命令。

项目主页:http://savannah.gnu.org/projects/screen