Overview
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 ISO 6429 (ECMA 48, ANSI X3.64) 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 user’s terminal.
Why Screen
灵活的会话管理,这里的会话可以理解成多个虚拟terminal窗口,你可以在一个screen中管理(切换、attach、detach、wipe等)多个用途的虚拟terminal,每一个被detach的terminal可以看成是一个后台进程窗口,并且即使后台进程中运行的程序挂掉了,只要screen程序不终结,此后台进程窗口就不会消失,你可以随时取回此窗口重启窗口中的运行程序或者清理掉它。
最佳使用场景:长链接操作,如大文件传输、web server运行终端等
Screen 用法
LM-SHB-24502050:Desktop cyang1$ screen --help
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]Options:
-aForce all capabilities into each window's termcap.
-A -[r|R]Adapt all windows to the new display width & height.
-c fileRead configuration file instead of '.screenrc'.
-d (-r)Detach the elsewhere running screen (and reattach here).
-dmS nameStart as daemon: Screen session in detached mode.
-D (-r)Detach and logout remote (and reattach here).
-D -RRDo whatever is needed to get a screen session.
-e xyChange command characters.
-fFlow control on, -fn = off, -fa = auto.
-h linesSet the size of the scrollback history buffer.
-iInterrupt output sooner when flow control is on.
-listor -ls. Do nothing, just list our SockDir.
-LTurn on output logging.
-mignore $STY variable, do create a new screen session.
-OChoose optimal output rather than exact vt100 emulation.
-p windowPreselect the named window if it exists.
-qQuiet startup. Exits with non-zero return code if unsuccessful.
-rReattach to a detached screen process.
-RReattach if possible, otherwise start a new session.
-s shellShell to execute rather than $SHELL.
-S socknameName this session .sockname instead of ...
-t titleSet title. (window's name).
-T termUse term as $TERM for windows, rather than "screen".
-UTell screen to use UTF-8 encoding.
-vPrint "Screen version 4.00.03 (FAU) 23-Oct-06".
-wipeDo nothing, just clean up SockDir.
-xAttach to a not detached screen. (Multi display mode).
-XExecute as a screen command in the specified session.
用法示例
假定我们需要使用screen创建一个会话用来跑一个node server,server启动之后需要将虚拟终端detach然后时不时的attach回来查看一下状况,那么在这个过程中的操作如下:
LM-SHB-24502050:tmp cyang1$ ls
server.js
LM-SHB-24502050:tmp cyang1$ screen -S nodeServer //启动了一个名为nodeServer的虚拟终端窗口,然后就会出现如下bash,我们在这个虚拟bash里启动server
文章图片
此时按下 C-a d 暂时断开screen会话,然后就可以查看到此会话就像后台进程一样运行着,然后使用screen -r nodeServer就可以恢复窗口,并且程序运行正常
[detached]
LM-SHB-24502050:tmp cyang1$ screen -ls #查看所有screen会话
There is a screen on:
78584.nodeServer (Detached)
1 Socket in /var/folders/xj/6k0xl7k54lj4r9r2cz5hn8h00000gp/T/.screen.LM-SHB-24502050:tmp cyang1$ screen -r nodeServer
注意 -r 与 -x的区别:
-rReattach to a detached screen process.(连接到一个在其他地方断开的会话)
-x Attach to a not detached screen. (Multi display mode).(连接到一个没有断开的会话,即会话共享)
如果一个会话被杀掉了,你想清除它应该怎么办呢:
screen -wipe
【Linux Screen 命令使用笔记】
文章图片
因为本人就用到了这些简单的功能,所以就不多写自己没用过的特性了,有兴趣参考官方手册
参考链接:
https://www.ibm.com/developerworks/cn/linux/l-cn-screen/index.html
http://www.gnu.org/software/screen/manual/html_node/Invoking-Screen.html#Invoking-Screen