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

<think>好的,用户想知道如何正确断开SSH远程连接,同时保持程序继续运行。首先,我需要回忆相关的知识,根据提供的引用内容,有几种方法可以实现这一点。 首先,用户可能在使用SSH时直接关闭终端窗口,但这样会导致正在运行的程序终止。因此,正确的断开方法需要确保进程在后台持续运行。引用中提到了几种方法,比如使用nohup结合&,或者使用screen、tmux这样的终端复用工具。 接下来,我需要整理这些方法,并按照清晰的步骤进行说明。比如,nohup的用法是基本的,用户只需要在命令前加上nohup,然后重定向输出,最后用&放入后台。这可能适用于简单的任务。而screen和tmux则更适合需要交互式操作的场景,允许用户重新连接到之前的会话。 另外,用户可能也想知道如何正常退出SSH会话而不影响后台进程。比如使用exit命令或者快捷键Ctrl+D,但这通常会导致关联的进程被终止,除非已经将进程放入后台并剥离了会话。 需要确保回答中提到的每种方法都正确引用对应的参考文献,比如使用nohup参考引用4,screen参考引用3,tmux虽然没有直接引用,但根据常见知识可以补充。同时,断开连接的具体步骤如关闭窗口或使用快捷键,需要参考引用2和3。 用户的问题可能还涉及如何检查进程是否在后台运行,所以应该包含相关的命令如ps或jobs。此外,可能存在的误区是直接关闭窗口导致进程终止,因此需要强调正确使用后台运行的方法。 最后,生成的相关问题需要围绕保持进程运行的方法、工具比较以及检查后台进程的命令,这些都能帮助用户进一步了解SSH断开后的进程管理。</think>### 如何正确断开SSH远程连接并保持程序运行? #### 1. 使用 `nohup` 命令(非交互式场景) - **步骤**: 1. 在运行命令前添加 `nohup`,并通过 `&` 将任务放入后台: ```bash nohup your_command > output.log 2>&1 & ``` 此命令会将标准输出和错误输出重定向到 `output.log` 文件[^4]。 2. 直接关闭SSH窗口或输入 `exit` 退出会话,任务仍会在服务器后台运行。 #### 2. 使用 `screen` 工具(交互式场景) - **步骤**: 1. 安装screen(如未预装): ```bash sudo apt-get install screen # Debian/Ubuntu ``` 2. 创建新会话: ```bash screen -S your_session_name ``` 3. 在会话中运行命令后: - 按 `Ctrl + A`,再按 `D` 分离会话(detach)[^3]。 4. 重新连接会话: ```bash screen -r your_session_name ``` #### 3. 使用 `tmux` 工具(更强大的终端复用) - **步骤**: 1. 启动tmux会话: ```bash tmux new -s session_name ``` 2. 运行命令后分离会话: - 按 `Ctrl + B`,再按 `D`。 3. 重新连接: ```bash tmux attach -t session_name ``` #### 4. 直接断开SSH连接的**错误方式** - ❌ 直接关闭终端窗口或按 `Ctrl + C`:会导致前台进程终止。 - ❌ 未使用后台运行命令时输入 `exit`:会发送SIGHUP信号终止进程[^1]。 #### 5. 验证进程是否存活 - 通过 `ps` 命令检查: ```bash ps aux | grep your_command ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yangshun_cug

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

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

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

打赏作者

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

抵扣说明:

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

余额充值