"); //-->
本文分享自天翼云开发者社区《linux-screen命令使用》,作者:SummerSnow
背景说明有时候我们需要在服务器上执行一个可能耗费几天或者更长时间的命令或脚本,在此过程中,如果中途断网或者远程服务器SSH超时断开,可能就会出现“前功尽弃”的局面,为了有效解决这种场景问题,我们的主角screen就闪亮登场了。
screen 是一个在Unix和类Unix系统上的终端复用工具。它允许用户在单个终端窗口中运行多个终端会话,并提供了一些其他功能,如会话断开后的恢复和远程连接的分离,以下是 screen 的一些主要特点:
多窗口:screen 允许在一个终端窗口中创建多个虚拟终端(窗口),每个窗口可以运行独立的命令和程序;
会话持久性:即使断开与服务器的连接,screen 会话仍然保持运行;
远程连接:screen 允许用户在同一会话中分离(detach)并重新连接,这对于长时间运行的任务或需要断开连接的远程会话非常有用;
多用户共享:多个用户可以连接到同一个 screen 会话,实现共享终端的目的;
屏幕分割:screen 允许用户在同一个窗口中分割屏幕,显示多个区域,每个区域可以运行不同的命令等。
本环境为CentOS Linux release 7.9.2009 (Core)部署
#1 如果您系统里没有screen命令,需要先安装screen yum install -y screen #2 命令验证(使用方法) [root@es1][~/python] $screen --helpUse: 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.参数说明
-4仅将主机名解析为IPv4地址。
-6仅将主机名解析为IPv6地址。
-a强制将所有功能放入每个窗口的termcap。
-A -[r|R] 使所有窗口适应新的显示宽度和高度。
** -c file 读取配置文件而不是 screenrc。**
-d(-r)分离别处运行的屏幕(并在此处重新连接)。
-dmS name作为守护进程启动:分离模式下的屏幕会话。
-D(-r)分离并注销远程(并在此处重新连接)。
-D -RR做任何需要的事情来获得screen会话。
-e xy更改命令字符。
-f **流量控制打开,-fn=关闭,-fa=自动。**
-h lines设置回滚历史缓冲区的大小。
-i流量控制开启时,中断输出更快。
-l登录模式打开(更新/var/run/utmp),-ln=off。
-ls [match]什么都不做,只列出我们的SockDir[关于可能的匹配],同-list
-list什么都不做,只列出我们的SockDir[关于可能的匹配]。
-L打开输出日志记录。
-m忽略STY变量,创建一个新的屏幕会话。
-O选择最佳输出,而不是精确的vt100模拟。
-p windows如果命名窗口存在,则预选该窗口。
-q安静启动。如果不成功,则使用非零返回代码退出。
-Q命令将向查询进程的标准输出发送响应。
-r [session]重新连接到分离的屏幕进程。
-R如果可能,重新连接,否则启动新会话。
-s shell要执行的shell。
-S sockname将此会话命名为**.sockname而不是...。**
-t title设置标题。(窗口名称)。
-T term将术语用作windows的$term,而不是“screen”。
-U告诉screen使用UTF-8编码。
-v打印“屏幕版本4.01.00devel(GNU)2006年5月2日”。
-wipe [match]什么都不做,只需清理SockDir。
-x连接到未分离的屏幕上。(多显示模式)。
-X在指定会话中作为屏幕命令执行。
#1 在shell终端直接使用screen命令,则会进入到一个screen里。 #1.1 打开screen,模拟ping百度screen[root@es1][~] $ screen[root@es1 ~]# ping baidu.com 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=51 time=7.56 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=51 time=6.64 ms#1.2 可以查看已打开的screen会话screen -ls [root@es1 ~]# screen -ls There is a screen on: 15346.pts-0.es1 (Attached) 1 Socket in /var/run/screen/S-root.#2.1 退出(不间断退出)ctrl+a,再按d,退出当前screen。#2.2 直接退出exit#3 退出后,如果想再次登入某个screen会话,可以使用的是screen -r [编号],这个编号指的是上述ls的编号,打开后就可以看到ping baidu.com一直在运行[root@es1][~] $ screen -r 15346 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=198 ttl=51 time=7.33 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=199 ttl=51 time=6.63 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=200 ttl=51 time=6.91 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=201 ttl=51 time=7.17 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=202 ttl=51 time=8.71 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=203 ttl=51 time=6.93 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=204 ttl=51 time=6.55 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=205 ttl=51 time=6.62 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=206 ttl=51 time=7.89 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=207 ttl=51 time=6.44 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=208 ttl=51 time=8.33 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=209 ttl=51 time=6.83 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=210 ttl=51 time=6.41 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=211 ttl=51 time=6.43 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=212 ttl=51 time=6.45 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=213 ttl=51 time=6.64 ms 64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=214 ttl=51 time=6.42 ms#4 在使用exit后再次查看screen -ls[root@es1 ~]# exit [root@es1][~] $ screen -lsNo Sockets found in /var/run/screen/S-root.多个screen不指定名字
#1 创建三个screen,若想进入到其中一个,只需指定id[root@es1][~]$ screen [detached from 16976.pts-0.es1][root@es1][~]$ screen [detached from 17011.pts-0.es1][root@es1][~]$ screen [detached from 17027.pts-0.es1]#2 查看screen ,目前已经有3个了[root@es1][~]$ screen -ls There are screens on: 16976.pts-0.es1 (Detached) 17011.pts-0.es1 (Detached) 17027.pts-0.es1 (Detached)3 Sockets in /var/run/screen/S-root.指定名字
#1 指定名字 [root@es1][~]$ screen -S "song_screen" [detached from 17462.song_screen]#2 列出所有的screen[root@es1][~]$ screen -ls There are screens on: 16976.pts-0.es1 (Detached) 17011.pts-0.es1 (Detached) 17027.pts-0.es1 (Detached) 17462.song_screen (Detached)4 Sockets in /var/run/screen/S-root. #3 进入指定screen,即可使用编号,也可使用名称[root@es1][~]$ screen -r 17462[detached from 17462.song_screen][root@es1][~]$ screen -r song_screen[detached from 17462.song_screen]
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。