screen命令

screen 命令在后台启动一个终端,可以从它分离然后重新连接。
这在我们远程登录系统时特别有用。
我们可以启动screen、启动命令、从screen分离和注销。
然后我们可以稍后登录并重新连接到screen并查看程序正在运行。

以下是 screen 命令可用的选项:

# screen --help
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]
Options:
-4            Use IPv4.
-6            Use IPv6.
-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.
-list         or -ls. Do nothing, just list our SockDir.
-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.
-r            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.00.03 (FAU) 23-Oct-06".
-wipe         Do nothing, just clean up SockDir.
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute [cmd] as a screen command in the specified session.
如何在 Linux 中使用“screen”命令

列出正在运行的screen

要列出screen及其状态(添加/分离),我们使用 -ls 选项的 -list。

# screen -ls
There are screens on:
        25045.pts-0.node01       (Detached)
        25168.pts-0.node02       (Detached)
2 Sockets in /var/run/screen/S-root.

启动screen会话

要启动screen会话,执行以下命令:

# screen

运行 screen 命令后,我们可以正常运行任何应用程序。
当我们运行screen时,会在后台启动一个终端命令。

重新连接到screen

要重新连接到screen,执行以下命令:

# screen -r
更多: zhilu jiaocheng

从screen上分离

要从screen上分离,请依次按以下键。
当会话分离时,我们将收到一条消息“[分离]”。
后台终端与此分离。

Ctrl+a+d
[detached]

如果你想从另一个终端分离screen,你也可以使用 -d 选项。
这里需要指定可以从 screen -ls 命令获取的screen ID。

# screen -d [screen id]

例如 :

# screen -ls
There are screens on:
        25168.pts-0.node01       (Attached)
1 Socket in /var/run/screen/S-root.
# screen -d 25168.pts-0.node01
[25168.pts-0.node01 detached.]
日期:2020-09-17 00:13:59 来源:oir作者:oir