Linux下SSH远程连接断开后,通过screen命令让远程执行程序在后台运行

1. screen简介

我们会经常使用xshell等远程连接Linux系统进行命令操作。当工作的数据量很大或者工作要持续很长的时间,我们就必须保证这个远程终端的启动,一旦终端关闭了,它所运行的进程也会关闭,我们所做的工作便无效。但是,即使我们不主动的关闭终端,终端有的时候也会应为一些原因(网络、锁屏等)而中断,导致我们的工作进度清零。

这个时候我们就可以用screen命令来解决这个问题了。

在screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。

也就是说:
远程服务器的时候,断网或者手误关掉了远程终端,会导致会话中断,程序终止。
而Screen连接的终端,会话独立运行,程序会一直进行。而且会话可以恢复,还可以自行删除。

(参考自:Linux–screen远程必备 #screen常用命令-会话的创建、恢复、删除(&重命名删除)用法,他写的挺好!!!)

2. screen安装

GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。

GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

通过以下命令安装screen:

$ sudo apt-get install screen

详细安装可参考:screen命令详解

3. screen用法

3.1 查看用法

screen功能较多,可以终端输入$ screen --help查看:

$ 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.
-Logfile file Set logfile name.
-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.06.02 (GNU) 23-Oct-17".
-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.

3.2 详细用法

详细用法可参考:linux screen 命令详解(这个人很专业的样子,但是不够通俗)

但是他说的太全面详细了,我们一般用不了这么多。下面介绍几个常见用法。

3.3 常用功能

这里说几个常用功能。

3.3.1 创建会话

用法:screen -S name
name就是你创建的会话名称,具体如下:

$ screen -S test

然后会清屏,其实是进入了一个名叫test的新会话。

3.3.2 列出当前会话

用法:screen -ls

想看看现在有那些会话,并且各自是什么状态。

$ screen -ls
There are screens on:
	27598.test2	(2021年01月30日 20时15分39秒)	(Detached)
	27564.test	(2021年01月30日 20时14分02秒)	(Detached)
	27458.pts-1.so-Precision-7920-Tower	(2021年01月30日 19时38分53秒)	(Attached)
	26734.shundownload	(2021年01月30日 16时19分30秒)	(Attached)
4 Sockets in /run/screen/S-so.

显示当前有4个会话,两个暂时断开(Detached),两个在连接(Attached)。

3.3.3 暂时断开会话

用法:screen -d name

比如现在我要关手边的电脑出去吃个饭,害怕这边程序中途断掉,就可以选择暂时断开会话。

$ screen -d shundownload 
[26734.shundownload detached.]

这个shundownload就是刚才在连接(Attached)的其中一个会话,现在暂时被断开(detached)了,但是还在后台运行。

$ screen -ls
There are screens on:
	27598.test2	(2021年01月30日 20时15分39秒)	(Detached)
	27564.test	(2021年01月30日 20时14分02秒)	(Detached)
	27458.pts-1.so-Precision-7920-Tower	(2021年01月30日 19时38分53秒)	(Attached)
	26734.shundownload	(2021年01月30日 16时19分30秒)	(Detached)
4 Sockets in /run/screen/S-so.

现在查看,只剩一个在连接的了。

3.3.4 返回会话

用法:screen -r name

现在我吃完饭回来了,看看进程到哪一步了,

$ screen -r shundownload

然后就回到进程了(shundownload状态显示为Attached)。

$ screen -ls
There are screens on:
	27598.test2	(2021年01月30日 20时15分39秒)	(Detached)
	27564.test	(2021年01月30日 20时14分02秒)	(Detached)
	27458.pts-1.so-Precision-7920-Tower	(2021年01月30日 19时38分53秒)	(Attached)
	26734.shundownload	(2021年01月30日 16时19分30秒)	(Attached)
4 Sockets in /run/screen/S-so.

3.3.5 结束当前会话,回到另一个会话

用法:screen -d -r name

结束当前会话,回到另一个会话name,就类似切换账号。

3.3.6 彻底删除会话(谨慎)

用法:screen -S name -X quit

这个会话name没用了,我想彻底把它删除,就用这个了。
删除需谨慎,一旦删除,后台运行程序就终止了。

END

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangshun_cug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值