The screen
is a terminal multiplexer that allows you to create multiple virtual terminals within a single physical terminal.
screen
is a very useful tool for managing long-running processes and tasks in an SSH session, especially when there's a risk of the session being terminated by network issues, server policies, or other disruptions.
When you start a screen
session, the processes running within that session are not tied to the SSH session.
If an SSH session is terminated for any reason, any screen
sessions running within that SSH session will automatically detach. The processes running inside the screen
session will continue to run in the background.
You can later reattach to the screen
session and resume your work where you left off.
After reconnecting via SSH, you can reattach to the detached screen
session using:
screen -r [session_id_or_name]
- Start a Screen Session
- Run Your Long-Running Process
- Detach from Screen
- Reattach to Screen
- Starting a new screen session
- Listing sessions
- Attaching (Resume) to an existing session
- Detaching from a session
- Closing a screen session
- Managing Virtual Windows
- Scroll mode / Scrolling
- Displaying help
- Exiting a screen session
- Killing all windows and terminating screen
- Configuration file
- Troubleshooting
Here’s a brief example of how you might use screen
in this scenario:
screen -S mysession
Start your task or process within the screen
session.
If you need to, you can detach from the screen
session by pressing Ctrl-a d
. The processes running within the screen
session will continue even if you are detached.
If your SSH session is terminated, you can SSH back into the server and reattach to the screen
session with:
screen -r mysession
This way, screen
can indeed help prevent data loss due to terminated SSH sessions by allowing processes to run uninterrupted and preserving your command-line environment between connections.
Here are some basic usage modes:
screen
or you can name (Substitue) the session (it has already a name [pid.tty.host]
):
screen -S session_name
screen -ls
or
screen -list
screen -r [pid.tty.host]/[session_name]
or simply (if there is only one)
screen -r
To detach from the current session, use the Ctrl-a d
keyboard shortcut.
After detaching from a session, you can close the session with the following command (eXecute):
screen -X -S [session_id] quit
- Create a new window:
Ctrl-a c
- Switch between windows:
Ctrl-a n
for the Next window, andCtrl-a p
for the Previous window - Switch by window number:
Ctrl-a [number]
- Naming a window:
Ctrl-a A
Activate scroll mode with Ctrl-a [
and use the arrow keys to scroll. To exit scroll mode, press Enter
or Esc
.
If you need more commands, press Ctrl-a ?
to access the help.
To exit and close the session, type exit
in the prompt.
To kill all windows and terminate screen, press Ctrl-a \
.
Or
- Press
Ctrl-a
to enter command mode. - Then type
:quit
and pressEnter
.
This will quit the screen
session by closing all windows at once. Please note that this will immediately terminate all processes running in each window, so make sure to save any unsaved work before using this command.
The configuration file for screen
is ~/.screenrc
. Here, you can customize the settings for screen
.
Example:
startup_message off
defscrollback 100000
shell -${SHELL}
#https://lizdenys.com/journal/articles/understanding-gnu-screens-captions.html
#https://www.gnu.org/software/screen/manual/html_node/String-Escapes.html
caption always '%{= kw}[ %{y}%H%{-} ][ %= %-Lw%{+b M}%n%f* %t%{-}%+LW %= ][ %{r}%l%{-} ][ %{c}%c%{-} ]'
screen -t "bash" 0 bash
screen -t "man" 1 bash
screen -t "nvim" 2 nvim
screen -t "mc" 3 mc
select 0
These are the most important commands and functions to use screen
effectively.
SSH session terminated by server remotely. When you log on again, you get this:
$ screen -ls
There is a screen on:
40383.pts-36.host001 (Attached)
1 Socket in /tmp/uscreens/S-noname.
Given that the screen session is still marked as "Attached", you won’t be able to directly reattach to it. However, you can forcefully reattach the session using the following command:
$ screen -d -r 40383.pts-36.host001
In this command:
- The
-d
option tellsscreen
to detach the session if it is still attached. - The
-r
option is used to reattach to a session. You will need to specify the session identifier after-r
, which in this case is40383.pts-36.host001
.
After running this command, you should be reconnected to your screen session, and any processes or tasks that were running in it will continue to do so.
If the display of your screen
session is garbled or disturbed, you can try a couple of things to redraw or refresh the interface:
-
Redraw Window: Press
Ctrl-a
followed byCtrl-l
. This command sends a redraw signal to the terminal, and it should clean up the display. -
Redraw Using Reset: Type the
reset
command in the terminal and pressEnter
. This command will reset and reinitialize the terminal, which can often clear up display issues. -
Resize Window: Sometimes resizing the terminal window can force it to redraw and correct any display issues.
Remember that if you are using any text-based applications with a user interface (like text editors), they may have their own redraw or refresh commands. For instance, in vim
, you can press Ctrl-l
to redraw the interface.