Screen: Difference between revisions
m Text replacement - "{{Navbox Linux}}{{Navbox Linux}}" to "{{Navbox Linux}}" |
mNo edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
GNU Screen is a terminal multiplexer which allows multiple terminals to run within a single session. Screen includes many useful features including serial terminal support, the ability to lock, detach, and reattach a running terminal session, and much more. | |||
== | Nowadays, it seems like [[tmux]] is the more popular terminal multiplexer and is what I use most often now. Nonetheless, Screen is still extremely useful especially if I just need to run something in the background. | ||
{{highlight|lang= | == Quick usage guide == | ||
After you have Screen installed using your distro's package manager, you can quickly get started with screen by learning a few basic commands. | |||
=== Starting a screen session === | |||
Start a new screen session by running <code>screen</code>. You should also pass in a custom name for the screen with the <code>-S</code> option which will help you identify and re-attach to the screen session later. | |||
{{highlight|lang=terminal|code= | |||
$ screen -S test | |||
}} | }} | ||
This will start a new shell instance inside screen. You can see your current screen session with the custom name by running: | |||
{{highlight|lang= | {{highlight|lang=terminal|code= | ||
$ screen -ls | |||
There are screens on: | |||
30922.test (Attached) | |||
}} | }} | ||
The state of the session is 'Attached' since we're in that screen session. Any jobs you run in this screen session can persist even if you log out if you detach from this screen session. | |||
== | === Detach from and re-attaching to an existing screen session === | ||
Detach from a screen session by hitting <code>Ctrl-a d</code>. This key combination is written as {{code|C-a d}} in the man pages. If you list the screens running again, you can see that the screen is detached. | |||
{{highlight|lang=terminal|code= | |||
$ screen -ls | |||
{{highlight|lang= | There are screens on: | ||
30922.test (Detached) | |||
}} | }} | ||
You may reconnect to detached screens by their partial name or PID number. | |||
{{highlight|lang=terminal|code= | |||
$ screen -r 3 # by the partial (but unique) PID number | |||
$ screen -r 30922 # or by the full PID number | |||
{{highlight|lang= | $ screen -r t # by the partial (but unique) screen name | ||
$ screen -r test # or by the full screen name | |||
}} | }} | ||
=== Navigating through scrollback history in screen === | |||
{{code|Ctrl-a [}} | Scrollback history in a screen terminal isn't as easy to work with as a typical terminal because there is no scrollbar that you can interact with. Instead, to navigate around through the scrollback history, you will have to first hit {{code|Ctrl-a [}} and then navigate with vi-like keys as outlined in the following table. | ||
{| class="wikitable" | {| class="wikitable" | ||
! Key | ! Key | ||
! Movement | ! Movement | ||
|- | |- | ||
| h || Move the cursor left by one character | | <code>h</code> || Move the cursor left by one character | ||
|- | |- | ||
| j || Move the cursor down by one line | | <code>j</code> || Move the cursor down by one line | ||
|- | |- | ||
| k || Move the cursor up by one line | | <code>k</code> || Move the cursor up by one line | ||
|- | |- | ||
| l || Move the cursor right by one character | | <code>l</code> || Move the cursor right by one character | ||
|- | |- | ||
| 0 || Move to the beginning of the current line | | <code>0</code> || Move to the beginning of the current line | ||
|- | |- | ||
| $ || Move to the end of the current line. | | <code>$</code> || Move to the end of the current line. | ||
|- | |- | ||
| G || Moves to the specified line (defaults to the end of the buffer). | | <code>G</code> || Moves to the specified line (defaults to the end of the buffer). | ||
|- | |- | ||
| C-u || Scrolls a half page up. | | <code>C-u</code> || Scrolls a half page up. | ||
|- | |- | ||
| C-b || Scrolls a full page up. | | <code>C-b</code> || Scrolls a full page up. | ||
|- | |- | ||
| C-d || Scrolls a half page down. | | <code>C-d</code> || Scrolls a half page down. | ||
|- | |- | ||
| C-f || Scrolls the full page down. | | <code>C-f</code> || Scrolls the full page down. | ||
|- | |- | ||
| / || Search forward | | <code>/</code> || Search forward | ||
|- | |- | ||
| ? || Search backward | |<code>?</code> || Search backward | ||
|} | |} | ||
If you ever need help finding the key bindings for all the different commands in Screen, hit <code>Ctrl-a ?</code> which will bring up the key bindings screen. | |||
== Rename Screen Session == | == Tips & Tricks == | ||
=== Rename Screen Session === | |||
In a screen terminal, enter {{code|Ctrl-a :sessionname}} followed by the new name. | In a screen terminal, enter {{code|Ctrl-a :sessionname}} followed by the new name. | ||
| Line 79: | Line 87: | ||
}} | }} | ||
== Lock a Session == | === Lock a Session === | ||
You can use screen to lock the virtual terminal by using the {{code|Ctrl-x}} key. | You can use screen to lock the virtual terminal by using the {{code|Ctrl-x}} key. | ||
== Running a command detached == | === Running a command detached === | ||
To run a command in a screen session in the background, use | To run a command in a screen session in the background using the {{code|-dm}} flag to specify the command. | ||
{{code | {{highlight|lang=terminal|code= | ||
~ % screen -S test -dm sleep 60 | |||
~ % screen -ls | |||
There are screens on: | |||
353.test (Detached) | |||
}} | |||
For more complex commands, you may want to wrap the complexity within {{code|sh -c}}. For instance, to use {{code|&&}} or to do pipe redirections, enclose it within a subshell. | |||
{{highlight|lang=terminal|code= | |||
~ % screen -S test -dm sh -c "sleep 60 && echo hello" | |||
~ % screen -ls | |||
There are screens on: | |||
353.test (Detached) | |||
}} | |||
== Running screen through su == | === Running screen through su === | ||
If you are running screen not directly through the terminal (such as through su), you will get the following error message: | If you are running screen not directly through the terminal (such as through su), you will get the following error message: | ||
| Line 100: | Line 121: | ||
}} | }} | ||
== Serial == | === Serial Console === | ||
You can use screen as a serial console as well. | You can use screen as a serial console as well. | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 107: | Line 128: | ||
Common baud rates are 9600 or 115200. | Common baud rates are 9600 or 115200. | ||
== Configuration == | |||
=== Customizing Screen using screenrc === | |||
When screen is started, it will read any configuration defined in <code>/etc/screenrc</code> and <code>~/.screenrc</code>. You can customize screen by modify the either of these configuration files. System defaults should go into the <code>/etc/screenrc</code> file while personalized settings should go into <code>~/.screenrc</code>. | |||
Here is a sample <code>~/.screenrc</code> configuration with customized status line and few additional options: | |||
{{Highlight | |||
| code = # Turn off the welcome message | |||
startup_message off | |||
# Disable visual bell | |||
vbell off | |||
# Set scrollback buffer to 10000 | |||
defscrollback 10000 | |||
termcapinfo xterm* OL=10000 | |||
# Change the xterm initialization string from is2=\E[!p\E[?3;4l\E[4l\E> | |||
# (This fixes the "Aborted because of window size change" konsole symptoms found | |||
# in bug #134198) | |||
termcapinfo xterm* 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l' | |||
# Customize the status line | |||
hardstatus alwayslastline | |||
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]' | |||
# Force Background Redraw | |||
defbce on | |||
| lang = text | |||
}} | |||
=== Other options === | |||
==== Scrollback Buffer ==== | |||
Scrollback buffer can be changed by editing {{code|.screenrc}}. Define the following line with the number of lines to scroll back. | |||
{{highlight|lang=text|code= | |||
defscrollback 5000 | |||
}} | |||
==== Disable Visual Bell ==== | |||
To disable the visual bell for the current screen instance, hit: | |||
{{highlight|lang=text|code= | |||
Ctrl+a Ctrl+g | |||
}} | |||
For a permanent fix, put the following into the {{code|~/.screenrc}} file | |||
{{highlight|lang=text|code= | |||
vbell_msg "bell: window ~%" # Message for visual bell | |||
vbellwait 2 # Seconds to pause the screen for visual bell | |||
vbell off # Turns visual bell off | |||
}} | |||
==== Force Background Redraw ==== | |||
Screen will sometimes mess with any background colors set by programs such as vim. This will cause weird visuals in the terminal where background colors are only rendered where there is text. | |||
You can force screen to redraw the entire area such that the background colors are preserved by putting the following in your {{code|.screenrc}} file: | |||
{{highlight|lang=text|code= | |||
defbce on | |||
}} | |||
== Troubleshooting == | |||
=== Screen terminates when SSH disconnects === | |||
Whenever SSH disconnects, the server will send a HANGUP signal to Screen. By default, Screen will handle this signal by detaching the session and preserving your running programs in the background. However, if you configured screen with the <code>autodetach off</code> option, screen will handle this signal by terminating the session. | |||
[[Category:Linux]] | If this is not the desired behavior and you ''do'' want to have screen keep your sessions when you disconnect, ensure you have <code>autodetach '''on'''</code> set.{{Navbox Linux}}[[Category:Linux]] | ||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Latest revision as of 07:10, 18 August 2023
GNU Screen is a terminal multiplexer which allows multiple terminals to run within a single session. Screen includes many useful features including serial terminal support, the ability to lock, detach, and reattach a running terminal session, and much more.
Nowadays, it seems like tmux is the more popular terminal multiplexer and is what I use most often now. Nonetheless, Screen is still extremely useful especially if I just need to run something in the background.
Quick usage guide
After you have Screen installed using your distro's package manager, you can quickly get started with screen by learning a few basic commands.
Starting a screen session
Start a new screen session by running screen. You should also pass in a custom name for the screen with the -S option which will help you identify and re-attach to the screen session later.
$ screen -S test
This will start a new shell instance inside screen. You can see your current screen session with the custom name by running:
$ screen -ls
There are screens on:
30922.test (Attached)
The state of the session is 'Attached' since we're in that screen session. Any jobs you run in this screen session can persist even if you log out if you detach from this screen session.
Detach from and re-attaching to an existing screen session
Detach from a screen session by hitting Ctrl-a d. This key combination is written as C-a d in the man pages. If you list the screens running again, you can see that the screen is detached.
$ screen -ls
There are screens on:
30922.test (Detached)
You may reconnect to detached screens by their partial name or PID number.
$ screen -r 3 # by the partial (but unique) PID number
$ screen -r 30922 # or by the full PID number
$ screen -r t # by the partial (but unique) screen name
$ screen -r test # or by the full screen name
Scrollback history in a screen terminal isn't as easy to work with as a typical terminal because there is no scrollbar that you can interact with. Instead, to navigate around through the scrollback history, you will have to first hit Ctrl-a [ and then navigate with vi-like keys as outlined in the following table.
| Key | Movement |
|---|---|
h |
Move the cursor left by one character |
j |
Move the cursor down by one line |
k |
Move the cursor up by one line |
l |
Move the cursor right by one character |
0 |
Move to the beginning of the current line |
$ |
Move to the end of the current line. |
G |
Moves to the specified line (defaults to the end of the buffer). |
C-u |
Scrolls a half page up. |
C-b |
Scrolls a full page up. |
C-d |
Scrolls a half page down. |
C-f |
Scrolls the full page down. |
/ |
Search forward |
? |
Search backward |
If you ever need help finding the key bindings for all the different commands in Screen, hit Ctrl-a ? which will bring up the key bindings screen.
Tips & Tricks
Rename Screen Session
In a screen terminal, enter Ctrl-a :sessionname followed by the new name.
Eg:
# screen -ls
There are screens on:
8570.test (Attached)
## Hit Ctrl-a :sessionname Hello
# screen -ls
There are screens on:
8570.Hello (Attached)
Lock a Session
You can use screen to lock the virtual terminal by using the Ctrl-x key.
Running a command detached
To run a command in a screen session in the background using the -dm flag to specify the command.
~ % screen -S test -dm sleep 60
~ % screen -ls
There are screens on:
353.test (Detached)
For more complex commands, you may want to wrap the complexity within sh -c. For instance, to use && or to do pipe redirections, enclose it within a subshell.
~ % screen -S test -dm sh -c "sleep 60 && echo hello"
~ % screen -ls
There are screens on:
353.test (Detached)
Running screen through su
If you are running screen not directly through the terminal (such as through su), you will get the following error message:
$ screen
Cannot open your terminal '/dev/pts/0' - please check.
You can remedy this using script:
$ script /dev/null
$ screen # Rerun script while in a typescript session
Serial Console
You can use screen as a serial console as well.
screen /dev/ttyS0 <baud>[,<options>]
Common baud rates are 9600 or 115200.
Configuration
Customizing Screen using screenrc
When screen is started, it will read any configuration defined in /etc/screenrc and ~/.screenrc. You can customize screen by modify the either of these configuration files. System defaults should go into the /etc/screenrc file while personalized settings should go into ~/.screenrc.
Here is a sample ~/.screenrc configuration with customized status line and few additional options:
# Turn off the welcome message
startup_message off
# Disable visual bell
vbell off
# Set scrollback buffer to 10000
defscrollback 10000
termcapinfo xterm* OL=10000
# Change the xterm initialization string from is2=\E[!p\E[?3;4l\E[4l\E>
# (This fixes the "Aborted because of window size change" konsole symptoms found
# in bug #134198)
termcapinfo xterm* 'is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l'
# Customize the status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
# Force Background Redraw
defbce on
Other options
Scrollback Buffer
Scrollback buffer can be changed by editing .screenrc. Define the following line with the number of lines to scroll back.
defscrollback 5000
Disable Visual Bell
To disable the visual bell for the current screen instance, hit:
Ctrl+a Ctrl+g
For a permanent fix, put the following into the ~/.screenrc file
vbell_msg "bell: window ~%" # Message for visual bell
vbellwait 2 # Seconds to pause the screen for visual bell
vbell off # Turns visual bell off
Force Background Redraw
Screen will sometimes mess with any background colors set by programs such as vim. This will cause weird visuals in the terminal where background colors are only rendered where there is text.
You can force screen to redraw the entire area such that the background colors are preserved by putting the following in your .screenrc file:
defbce on
Troubleshooting
Screen terminates when SSH disconnects
Whenever SSH disconnects, the server will send a HANGUP signal to Screen. By default, Screen will handle this signal by detaching the session and preserving your running programs in the background. However, if you configured screen with the autodetach off option, screen will handle this signal by terminating the session.
If this is not the desired behavior and you do want to have screen keep your sessions when you disconnect, ensure you have autodetach on set.