抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Screen用法学习

2025-04-09 11:17:38 sizaif

什么是 Screen 命令

screen 是一个多任务窗口管理器,它允许用户在单个终端窗口中创建多个会话,并在这些会话之间进行切换。它的最大特点是可以将正在运行的会话“脱离”当前的终端窗口,即使用户断开连接或者关闭终端,会话依然保持运行,用户可以在稍后重新连接并继续操作。对于那些需要在后台运行长时间任务的用户来说,screen 提供了极大的便利。

适合使用场景

  • 远程连接中的长时间任务

    在使用 ssh 远程连接服务器时,运行一些需要长时间执行的任务,比如数据库备份、文件传输、编译大规模项目等,常常因为网络不稳定或意外断开连接导致任务中断。通过 screen 命令,可以将这些任务置于后台继续运行,即使断开连接,任务也不会受到影响。
  • 管理多个任务

    在一个终端中同时运行多个任务是常见需求。通过 screen,用户可以在同一个会话中创建多个窗口,每个窗口独立运行不同的任务,极大地提高了效率。
  • 协同操作

    screen 还支持多个用户共享同一个会话。这在团队合作、远程教学或实时协同调试中非常有用。用户只需启动一个 screen 会话,然后其他用户可以通过相同的命令进入到该会话中一起操作。
  • 监控任务的实时输出

    有些时候我们需要监控一些脚本或服务的实时输出,确保它们正常运行。screen 允许我们在后台运行这些任务,并随时通过重新连接会话来检查它们的输出。

Screen 命令的主要功能

screen 命令的核心功能是提供一种虚拟的终端会话管理方式,具体来说包括以下几点:

  • 保持会话运行:当用户关闭当前的终端窗口,screen 会保持会话继续运行,这使得长时间任务在网络断开或意外中断时仍能顺利执行。
  • 会话的恢复:用户可以随时断开和重新连接一个会话,这种“可恢复性”对于远程管理非常有用。
  • 多会话管理:用户可以在一个终端窗口中运行多个独立的会话,并通过快捷键在不同会话之间自由切换。
  • 共享会话:多个用户可以共享一个会话,这对于需要协同操作的场景尤其有用。

Screen 的基础使用

启动一个新的 Screen 会话

要启动一个新的 screen 会话,只需在终端中输入 screen 命令即可:

screen

此时,您进入了一个新的 screen 会话,可以像平常一样执行命令。如果要退出该会话但不关闭它,可以使用 Ctrl + A 然后按 D(即“detach” 会话)。

恢复一个断开的 Screen 会话

当需要重新连接一个已经“脱离”的会话时,可以使用 screen -r 命令:

screen -r

如果有多个会话运行,可以通过 screen -ls 来查看当前正在运行的会话,并指定要恢复的会话 ID:

screen -ls
screen -r [会话ID]

退出一个 Screen 会话

当任务完成后,可以通过 exit 命令退出会话:

exit

这将关闭当前的会话窗口,并终止其中运行的进程。

Screen 命令的常用参数

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.08.00 (GNU) 05-Feb-20".
-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.

常用命令

-S XXX 指定会话名称ID
-r XXX 恢复ID会话
-d 强制脱离会话 
# 在某些情况下,您可能已经从某个终端连接了 screen 会话,此时想要从另一个终端连接该会话,可以使用 -d -r 参数强制脱离并恢复:screen -d -r mysession
-ls:列出所有会话

Screen进阶使用

在会话中创建新窗口

在一个 screen 会话中,用户可以通过快捷键 Ctrl + A 然后按 C 来创建一个新窗口。每个窗口都相当于一个独立的 shell 环境,用户可以在不同窗口中运行不同的任务。

在不同窗口间切换

screen 中可以使用 Ctrl + A 然后按数字键 09 来切换到指定的窗口,也可以使用 Ctrl + A 然后按 N 来切换到下一个窗口

关闭窗口

要关闭当前窗口,只需在窗口中运行 exit 命令。这个操作类似于退出一个普通的 shell 会话。

评论吧



本站总访问量为 访客数为

鲁 ICP 备 20018157 号-1
Copyright 2021 - 2022 sizaif. All Rights Reserved