linux-screen命令使用

天翼云开发者社区
• 阅读 3

本文分享自天翼云开发者社区《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 --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.

参数说明

-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​在指定会话中作为屏幕命令执行。

命令使用

单个screen

#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 -ls
No 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]
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
SFTP 自动输入密码短语
如果你的创建的密钥对设置了密码短语,使用ssh或者sftp命令时,提示手动输入密码短语。这时候如果是自动化的脚本作业,应该如何写呢?网上的方案有使用sshpass,但是如果服务器是AIX系统,很不幸,大概率是找不到这个命令的。ssh.com官网的sshadd(https://www.oschina.net/action/GoToLink?u
Wesley13 Wesley13
3年前
SSH 登录太慢的解决方法
SSH登录太慢可能是DNS解析的问题,默认配置下sshd初次接受ssh客户端连接的时候会自动反向解析客户端IP以得到ssh客户端的域名或主机名。如果这个时候DNS的反向解析不正确,sshd就会等到DNS解析超时后才提供ssh连接,这样就造成连接时间过长、ssh客户端等待的情况,一般为1030秒左右。有个简单的解决办法就
Wesley13 Wesley13
3年前
java打开本地应用程序(调用cmd)
有时候我们需要借助java程序打开电脑自带的一些程序,可以直接打开或者借助cmd命令窗口打开一些常用的应用程序或者脚本,在cmd窗口执行的命令都可以通过这种方式运行。例如:packagecn.xm.exam.test;importjava.io.IOException;importorg
Wesley13 Wesley13
3年前
NGINX 502 BAD GATEWAY的解决办法
出现502badgateway错误的原因1.phpfpm进程数不够用2.Linux内核打开文件数量小3.脚本执行时间超时4.缓存设置比较小网站间歇性出现502,第一反应不是程序的问题,而是nginx服务器的问题,因为这是代理服务器出现的问题,代理服务器并没有安装php排除第一中情况。于此想到的是可能是超时,所以我把超时修
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
3年前
Linux中标准输出和标准错误的重导向
如果一个命令需要长时间在服务器上运行,那么很多时候会用到nohup命令,这时即便远程登录ssh中断了与服务器的联系,那么在服务器上运行的命令也不会因此而被迫停止。通常情况下,nohup与&连用,&的意思是将该命令放在后台执行。如下:nohupexample.sh&将exmaple.sh通过&放在服务器后台运行,nohup确保了即便当前
Stella981 Stella981
3年前
Linux应急响应(一):SSH暴力破解
0x00前言SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全。SSH口令长度太短或者复杂度不够,如仅包含数字,或仅包含字母等,容易被攻击者破解,一旦被攻击者获取,可用来直接登录系统,控制服务器所有权限。0x01应急场景某天,网站
Stella981 Stella981
3年前
Linux系统中设置开机自动运行的两种方法
有时可能会需要在重启时或者每次系统启动时运行某些命令或者脚本。我们要怎样做呢?本文中我们就对此进行讨论。我们会用两种方法来描述如何在 CentOS(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.linuxprobe.com%2F)/RHEL以及Ubuntu系统上做到重启或
Stella981 Stella981
3年前
SpringBoot 开发秘籍
!(https://oscimg.oschina.net/oscnet/0b0e2e35748141a9a2de5f4eb579de18.png)概述在项目开发过程中,某个功能需要依赖在配置文件中配置的参数。这时候就可能出现下面这种现象问题:有时候经常出现项目启动了,等到使用某个功能组件的时候出现异常,提示参数未配置或者bean
芝士年糕 芝士年糕
2年前
windows开启ssh服务教程
推荐使用3A服务器安装Windows系统,首先可以先查看一下自己的电脑是否开启了SSH使用WinR,输入cmd,回车。打开WindowsPowerShell或者命令提示符窗口。输入ssh,如果出现图上内容代表你的电脑上已经安装了ssh。如果出现报错,提示错误等,就代表没有安装或者没有开启。安装并开启SSH1.通过电脑右下角打开设置。2.打开应用。2.在应
天翼云开发者社区
天翼云开发者社区
Lv1
天翼云是中国电信倾力打造的云服务品牌,致力于成为领先的云计算服务提供商。提供云主机、CDN、云电脑、大数据及AI等全线产品和场景化解决方案。
文章
898
粉丝
16
获赞
40