linux神技:screen命令管理远程会话

   使用场景


通常我们使用SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如爬虫、ftp 传输、文件备份等等。

通常情况下我们都是为每一个这样的任务单独新开一个远程终端窗口,因为它们执行的时间太长了。必须等待它们执行完毕,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了。

如果你不想开那么多窗口,让任务正常在后台运行,那么你需要学习一下screen命令。常用的命令很简单。你会很快学会的。

二  安装screen


Linux服务器    

yum install  screen    # Linux服务器

Ubuntu服务器   

sudo apt-get install screen   # Ubuntu服务器

三  尝试screen   新建screen窗口


简单来说,Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器。Screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的telnet/SSH连接窗口那样。在screen中创建一个新的窗口有这样几种方式:

1  直接在命令行键入screen命令

screen

Screen将创建一个执行shell的全屏session会话窗口。

这种方法查看的话,只能通过进程ID(pid)查看,如果screen窗口很多,会因为进程ID太多而混淆。如果只有一个screen的可以用这种方法。

个人推荐使用第二种(即下面这一种),在查看sceen列表的时候,可以用screen名字来区分。

2   给新建的screen命名后打开

screen  -S   test_screen

-S  后面加新screen窗口的名称。新建一个名叫 test_screen的session,并马上进入。

以上两种方法会新建一个session会话窗口。你可以执行任意shell程序,就像在ssh窗口中那样。在该窗口中键入exit退出该窗口,如果这是该session会话的唯一窗口,该session会话退出,否则screen自动切换到前一个窗口。

3  screen  加  程序启动命令

screen python run.py # python 启动 名为run.py的脚本

Screen创建一个执行 python run.py 的单窗口会话,退出的话(ctrl + c),将退出该窗口/会话。

4  在已有的session会话,创建新的session会话

我们还可以在一个已有screen会话中创建新的窗口。在当前screen窗口中键入C-a c,即Ctrl键+a键,之后再按下c键,screen 在该会话内生成一个新的窗口并切换到该窗口。

关键的地方来了,如何正确使用screen命令退出session会话,并保持程序后台正常运行呢?

四  screen退出和重新连接attach


1  screen的正确退出方法

C-a  d   即 按住 Ctrl键 和 a键 点击 d键 (下同)

退出到进入前的shell窗口

查看所有screen建立的session会话,使用:

screen  --list

上图  30033.test_screen 使用过 screen -S + 名字 新建的session  ,30033是对应的进程ID(pid)。下一行是直接screen建立的session会话,没有名称。

Detached表示已经断开连接。但是保持在后端运行。

2  screen 的重新连接

screen  -r  进程ID        #  screen  -r  30033 
# 或者
screen  -r   session名称    #  screen  -r  test_screen

3  screen被中断的清理

清理无效的session使用

screen -wipe

五  其他C-a绑定键和选项


1  C-a绑定键

2  其他选项

下面附上 screen  --help的信息:

Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]

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.
-c file Read configuration file instead of '.screenrc'.
-d (-r) Detach the elsewhere running screen (and reattach here).
-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.
-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].
-L Turn on output logging.
-m ignore $STY variable, do create a new screen session.
-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 Reattach if possible, otherwise start a new session.
-s shell Shell to execute rather than $SHELL.
-S sockname Name this session <pid>.sockname instead of <pid>.<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.
-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).
-X Execute <cmd> as a screen command in the specified session.

还可以参考的

GUN  screen 官网 

Screen的man page提供了最详细的信息

原文地址:https://www.cnblogs.com/haitianzhimen/p/10169071.html