New window screen linux

GNU Screen

GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. Programs running in Screen continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal.

See the official overview GNU Screen manual for the description of the features.

Installation

Usage

Commands are entered pressing the «escape key» Ctrl+a and then the key binding.

Some users find the default escape key Ctrl+a inconvenient. The escape key can be changed to another key as described in #Change the escape key.

Common Commands

  • Ctrl+a ? Displays commands and their defaults
  • Ctrl+a : Enter to the command prompt of screen
  • Ctrl+a » Window list
  • Ctrl+a 0 opens window 0
  • Ctrl+a A Rename the current window
  • Ctrl+a a Sends Ctrl+a to the current window
  • Ctrl+a c Create a new window (with shell)
  • Ctrl+a S Split current region horizontally into two regions
  • Ctrl+a | Split current region vertically into two regions
  • Ctrl+a tab Switch the input focus to the next region
  • Ctrl+a Ctrl+a Toggle between current and previous region
  • Ctrl+a Esc Enter Copy Mode (use enter to select a range of text)
  • Ctrl+a ] Paste text
  • Ctrl+a Q Close all regions but the current one
  • Ctrl+a X Close the current region
  • Ctrl+a d Detach from the current screen session, and leave it running. Use screen -r to resume

Command Prompt Commands

  • Ctrl+a :quit Closes all windows and closes screen session
  • Ctrl+a :source ~/.screenrc Reloads screenrc configuration file (can alternatively use /etc/screenrc )

Named sessions

To create a named session, run screen with the following command:

$ screen -S session_name 

To (re)name an existing a session, run the following command while screen is running:

Ctrl+a :sessionname session_name

To print a list of pid.tty.host strings identifying your screen sessions:

To attach to a named screen session, run this command:

$ screen -x session_name 
$ screen -r session_name 

Customizing Screen

You can modify the default settings for Screen according to your preference either through a personal .screenrc file which contains commands to be executed at startup (e.g. ~/.screenrc ) or on the fly in command mode (e.g. Ctrl+a :vbell off ).

Tips and tricks

Autostart with systemd

This service autostarts screen for the specified user (e.g. systemctl enable screen@florian ). Running this as a system unit is important, because systemd —user instance is not guaranteed to be running and will be killed when the last session for given the user is closed.

/etc/systemd/system/screen@.service
[Unit] Description=screen After=network.target [Service] Type=simple User=%i ExecStart=/usr/bin/screen -DmS autoscreen ExecStop=/usr/bin/screen -S autoscreen -X quit [Install] WantedBy=multi-user.target

Change the escape key

It can be a good idea to change the default escape key, not only because «a» is usually typed with the left pinky, but also because Ctrl+a is mapped to the common command beginning-of-line in GNU Readline and Bash-like shells.

Читайте также:  Linux жесткие мягкие ссылки

The escape key can be changed with the escape option in ~/.screenrc , or the -e option to screen .

For example, if you find that you rarely type Ctrl+j in your shell or editor, you could use escape ^Jj to set the escape key to Ctrl+j . The second «j» means that a literal Ctrl+j can be sent to the terminal via the sequence Ctrl+j j . For Dvorak keyboard users, Ctrl+t ( escape ^Tt ) might be more convenient.

More exotic options include escape « which sets the escape key to ` , or escape ^^^ which sets it to Ctrl+^ .

The escape key is also called the «command character» in Screen documentation.

Start at window 1

By default, the first screen window is 0. If you would rather never have a window 0 and start instead with 1, add the following lines on your configuration:

bind c screen 1 bind ^c screen 1 bind 0 select 10 screen 1

Nested Screen Sessions

It is possible to get stuck in a nested screen session. A common scenario: you start an SSH session from within a screen session. Within the SSH session, you start screen. By default, the outer screen session that was launched first responds to Ctrl+a commands. To send a command to the inner screen session, use Ctrl+a a , followed by your command. For example:

Start Screen on every shell

For Bash and Zsh, add the following snippet to your .bashrc or .zshrc before your aliases:

if [[ -z "$STY" ]]; then screen -xRR session_name fi

Use 256 colors

This article or section is out of date.

Reason: screen 4.4 sets $TERM to screen.$TERM , although the manual says something different. There should be no need to modify it in ~/.screenrc . (Discuss in Talk:GNU Screen)

By default, Screen uses an 8-color terminal emulator. To enable more colors, you need to be using a terminal that supports them and set the correct term value. This will use terminfo to describe how the ANSI escape codes will be interpreted. An entry in the terminfo database structure must exist, ncurses provides many common descriptions stored under /usr/share/terminfo/ .

First try the generic value:

If that does not work, try setting it based on your terminal. When using xterm-based terminal:

Note: /usr/share/terminfo/r/rxvt-unicode-256color is provided by rxvt-unicode-terminfo , which is installed as a dependency of rxvt-unicode . However, if you log into a server via SSH and run screen there, this terminfo file might not be available on the server. In this case it is recommended to copy /usr/share/terminfo/r/rxvt-unicode-256color on the server, it can be saved in ~/.terminfo/ .

As a last resort, try setting termcapinfo instead:

attrcolor b ".I" # allow bold colors - necessary for some reason termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm' # tell screen how to set colors. AB = background, AF=foreground defbce on # use current bg color for erased chars

Informative statusbar

The default statusbar may be a little lacking. You may find this one more helpful:

hardstatus off hardstatus alwayslastline hardstatus string '%[ %%H %][%= %%?%-Lw%?%(%%n*%f%t%?(%u)%?%)%%?%+Lw%?%?%= %][% %m-%d % %c %]'

Another possibility, taken from frodfrog’s blog [dead link 2023-04-23 ⓘ] is:

hardstatus alwayslastline '%[ %%H %][%= %%?%-Lw%?%%n*%f %t%?%(%u)%?%%+Lw%?%= %][ %Load: %l %][%%Y-%m-%d %%c:%s %]'

As of Screen v5 (master branch currently) the escape codes have changed, you could use this instead:

truecolors on hardstatus off backtick 0 5 5 "/bin/date" '+%Y-%m-%d' backtick 1 5 5 "/bin/date" '+%H:%M' hardstatus alwayslastline '%[ %H ][%%= %%?%-Lw%?%%(%%n%f%t%?(%u)%?%%)%%?%+Lw%?%? %=%][ %%%0` %%%1`% ]'

Turn welcome message off

Turn your hardstatus line into a dynamic urxvt|xterm|aterm window title

This one is pretty simple; just switch your current hardstatus line into a caption line with notification, and edit accordingly:

backtick 1 5 5 true termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007' hardstatus string "screen (%n: %t)" caption string "%%Y-%m-%d;%c %%-Lw%%[%n %t]%%%+Lw%1`" caption always

This will give you something like screen (0 bash) in the title of your terminal emulator. The caption supplies the date, current time, and colorizes your screen window collection.

Читайте также:  Linux find exclude file

Use X scrolling mechanism

The scroll buffer of GNU Screen can be accessed with Ctrl+a [ . However, this is very inconvenient. To use the scroll bar of e.g. xterm or Konsole, add the following line [1]:

termcapinfo xterm*|rxvt*|kterm*|Eterm* ti@:te@

Attach an existing running program to screen

If you started a program outside Screen, but now you would like it to be inside, you can use reptyr to reparent the process from its current TTY to one inside screen.

Get the PID of the process (you can use ps ax for that). Now just enter the PID as argument to reptyr inside a screen window.

Setting a different bash prompt while in screen

If you want a different bash prompt when in a screen session, add the following to your .bashrc [2]:

if [ -z $STY ] then PS1="YOUR REGULAR PROMPT" else PS1="YOUR SCREEN PROMPT" fi

Turn off visual bell

With this setting, Screen will not make an ugly screen flash instead of a bell sound.

Getting rid of the vertical and horizontal bars

To get rid of the vertical bars:

To hide the horizontal bar, set the back and foreground color to default (d) and display a blank (» «):

If this does not work, try caption string «% » instead. For the default caption in black and white, use caption string «%%3n %t» .

Troubleshooting

Fix for residual editor text

When you open a text editor like nano in screen and then close it, the text may stay visible in your terminal. To fix this, put the following:

Fix for Name column in windowlist only show «bash»

This article or section needs expansion.

Add following to ~/.screenrc :

See also

  • This page was last edited on 17 June 2023, at 17:59.
  • Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.
  • Privacy policy
  • About ArchWiki
  • Disclaimers

Источник

screen command in Linux with Examples

screen command in Linux provides the ability to launch and use multiple shell sessions from a single ssh session. When a process is started with ‘screen’, the process can be detached from session & then can reattach the session at a later time. When the session is detached, the process that was originally started from the screen is still running and managed by the screen itself. The process can then re-attach the session at a later time, and the terminals are still there, the way it was left. Syntax:

  • -a: It force all capabilities into each window’s termcap.
  • -A -[r|R]: It adapt all windows to the new display width & height.
  • -c file: It read configuration file instead of ‘.screenrc’.
  • -d (-r): It detach the elsewhere running screen (and reattach here).
  • -dmS name: It start as daemon: Screen session in detached mode.
  • -D (-r): It detach and logout remote (and reattach here).
  • -D -RR: It do whatever is needed to get a screen session.
  • -e xy: It change the command characters.
  • -f: It make the flow control on, -fn = off, -fa = auto.
  • -h lines: It set the size of the scrollback history buffer.
  • -i: It interrupt output sooner when flow control is on.
  • -l: It make the login mode on (update /var/run/utmp), -ln = off.
  • -ls [match]: It display all the attached screens.
  • -L: It turn on output logging.
  • -m: It ignore $STY variable, do create a new screen session.
  • -O: It choose optimal output rather than exact vt100 emulation.
  • -p window: It preselect the named window if it exists.
  • -q: It quiet startup. Exits with non-zero return code if unsuccessful.
  • -Q: It commands will send the response to the stdout of the querying process.
  • -r [session]: It reattach to a detached screen process.
  • -R: It reattach if possible, otherwise start a new session.
  • -S sockname: It name this session .sockname instead of …
  • -t title: It set title. (window’s name).
  • -T term: It use term as $TERM for windows, rather than “screen”.
  • -U: It tell screen to use UTF-8 encoding.
  • -v: It print “Screen version 4.06.02 (GNU) 23-Oct-17”.
  • -x: It attach to a not detached screen. (Multi display mode).
  • -X: It execute as a screen command in the specified session.
Читайте также:  Connection refused by server linux

Shortcut keys Options:

  • Ctrl-a + c: It create a new windows.
  • Ctrl-a + w: It display the list of all the windows currently opened.
  • Ctrl-a + A: It rename the current windows. The name will appear when you will list the list of windows opened with Ctrl-a + w.
  • Ctrl-a + n: It go to the next windows.
  • Ctrl-a + p: It go to the previous windows.
  • Ctrl-a + Ctrl-a: It back to the last windows used.
  • Ctrl-a + k: It close the current windows (kill).
  • Ctrl-a + S: It split the current windows horizontally. To switch between the windows, do Ctrl-a + Tab.
  • Ctrl-a + |: It split the current windows vertically.
  • Ctrl-a + X: Close active Split window
  • Ctrl-a + Q: Close all Split windows
  • Ctrl-a + d: It detach a screen session without stopping it.
  • Ctrl-a + r: It reattach a detached screen session.
  • Ctrl-a + [: It start the copy mode.
  • Ctrl-a + ]: It paste the copied text.
  • Installation of screen command: To install the screen command simply go to the terminal and type the following command:

  • screen: It will start a new window within the screen.

  • -S: It will start a new window within the screen and also gives a name to the window. It creates a session which is identified by that name. The name can be used to reattach screen at a later stage.

  • -ls: It is used to display the currently opened screens including those running in the background. It will list all the attached as well as detached screen sessions.

  • -d: It is used to detach a screen session so that it can be reattached in future. It can also be done with the help of shortcut key Ctrl-a + d

  • Here 1643 is the screen id we want to detach.
  • -r: It is used to reattach a screen session which was detached in past.

Источник

Оцените статью
Adblock
detector