banner
lca

lca

真正的不自由,是在自己的心中设下牢笼。

Terminal multiplexer screen tool usage record

Overview#

screen is a powerful terminal multiplexer that allows you to run multiple full-screen panes (virtual terminals) on a single physical terminal. This is a particularly useful tool, especially when you need to run long-running processes or keep sessions from being disconnected in remote sessions. screen allows applications to run in the background.

Installation#

Installation on Linux:

sudo apt install screen

Installation on Mac:

brew install screen

Options#

OptionDescription
-4Only resolve the hostname to an IPv4 address.
-6Only resolve the hostname to an IPv6 address.
-A -[r|R]Adapt all windows to the new display width and height.
-c fileRead the configuration file instead of '.screenrc'.
-d (-r)Detach the running screen (and reattach here).
-dmS nameStart as a daemon: run screen in detached mode.
-D (-r)Detach and logout remote (and reattach here).
-D -RRDo whatever is needed to get a screen session.
-fEnable flow control, -fn = off, -fa = auto.
-h linesSet the size of the scrollback buffer.
-iInterrupt output earlier when flow control is on.
-lTurn on login mode (update /var/run/utmp), -ln = off.
-ls [match]Do nothing, just list our SockDir [on possible matches].
-LTurn on output logging.
-Logfile fileSet the name of the log file.
-mIgnore $STY variable, create a new screen session.
-OChoose optimal output rather than exact vt100 emulation.
-p windowPreselect the named window if it exists.
-qQuiet startup. Exit with non-zero return code if unsuccessful.
-r [session]Reattach to a detached screen process.
-RReattach if possible, otherwise start a new session.
-s shellShell to execute, rather than $SHELL.
-S socknameName this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t titleSet the title (name) of the window.
-UTell screen to use UTF-8 encoding.
-xAttach to a not detached screen (multi display mode).
-XExecute <cmd> as a screen command in the specified session.

Examples#

  • Start a new screen session
screen
  • Name a screen session for easy session management
screen -S <name>
  • Detach a running screen session and put it in the background (within the session environment)
ctrl + a; d

image

  • List running screen sessions
screen -ls

image

  • Reconnect to a running screen by specifying the PID
screen -r <pid>

image

  • Reconnect to a running screen by specifying the name
screen -r <name>

image

  • Lock a screen session
ctrl + a; x

A password is required to open the session.

  • Kill a detached screen session
screen -S <name-or-pid> -X kill

image

  • Quit a detached screen session
screen -S <name-or-pid> -X quit

image

Common Shortcuts#

  • Create a new window in the current session
ctrl + a;c

Switching Windows

Switch to the next window:

ctrl-a;n

Switch to the previous window:

ctrl-a;p

List all windows:

ctrl-a;w

Kill a Window

Close the current window:

Ctrl-a;k

Rename a Window

Rename the current window:

Ctrl-a;A

Split Screen

Split the screen horizontally:

Ctrl-a;S

Split the screen vertically (supported in newer screen versions):

Ctrl-a;|

Switch to the next split screen:

Ctrl-a;TAB

Close the current split screen:

Ctrl-a;X

Practical Examples#

Remote Sessions#

If you need to operate through ssh on a remote server and want to keep the session from being interrupted, you can use screen:

ssh user@server_ip
screen -S remotesession

Perform operations within the screen session, and even if the network connection is interrupted, your session will continue to run. You can reconnect after reestablishing the connection:

ssh user@server_ip
screen -r remotesession

Multitasking#

Handle multiple tasks within the same screen session: screen -S multitask

Run a task in the first window, such as monitoring system logs: tail -f /var/log/syslog

Create a new window: Ctrl-a;c

Perform other tasks in the new window, such as editing a file: vim /etc/hosts

Switch between them using Ctrl-a;n and Ctrl-a;p.

Exiting a Screen Session#

After closing all windows with exit or Ctrl-d, the screen session will automatically end.

Cover image from: https://wallhaven.cc/w/x6yzoz

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.