首页 > 云计算 > 私有云实践 > linux-screen命令使用
本文分享自天翼云开发者社区《 linux-screen命令使用》,作者:SummerSnow
有时候我们需要在服务器上执行一个可能耗费几天或者更长时间的命令或脚本,在此过程中,如果中途断网或者远程服务器SSH超时断开,可能就会出现“前功尽弃”的局面,为了有效解决这种场景问题,我们的主角screen就闪亮登场了。
screen 是一个在Unix和类Unix系统上的终端复用工具。它允许用户在单个终端窗口中运行多个终端会话,并提供了一些其他功能,如会话断开后的恢复和远程连接的分离,以下是 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.
#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.
#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]
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70014251/viewspace-3093467/ ,如需转载,请注明出处,否则将追究法律责任。