Использование Screen оконного менеджера для терминала

Screen - терминальный мультиплексор, оконный менеджер для терминала. Screen - используется для управления процессами. Одна из основных особенностей программы screen заключается в том, что она позволяет отсоединяться от долгоживущего процесса и вновь возвращаться к нему.

Возможно, при работе через SSH вы сталкивались с проблемой обрыва соединения посреди работы с программой, из-за чего вы были вынуждены перезапустить её заново, чтобы досмотреть до конца её вывод. Именно поэтому я считаю, что для людей, подключающихся к удалённой машине более чем на минуту и выполняющих там более одной команды, жизненно необходимо использовать Screen или другой терминальный мультиплексор. И даже если вы как раз их тех, кто подключается на минутку и запускает только одну команду, всё равно Screen будет полезен. Используя Screen через SSH, вы сможете запустить команду, отсоединить сессию и отключиться от удалённой машины. Я рекомендую отсоединять сессию до разрыва SSH-соединения, но Screen должен сделать это самостоятельно. Потом вы просто снова подключаетесь к удалённой сессии и получаете все данные, что программа успела вывести за это время.

Все, что необходимо для запуска процесса под управлением программы screen, - это поместить команду screen перед командой запуска долгоживущего процесса, а затем нажать комбинации клавиш Ctrl+a, d, чтобы отсоединиться от сеанса.

Первый запуск Screen с заданием понятного имени, например darkfire

screen -S darkfire

# Проверяем
screen -ls

There is a screen on:
        21598.darkfire  (Attached)
1 Socket in /var/run/screen/S-darkfire.

После запуска вы увидите либо приглашение командной строки, либо «экран приветствия», нажав SPACE или ENTER в котором, вы так же попадете в shell. При этом все команды будут запущены уже «внутри» screen’а.

Подключение к запущенному

screen -rda name

если сессия одна, достаточно просто команды:

$  screen –x

если сессий несколько, то: посмотреть список запущенных screen’ов можно командой screen -ls:

$  screen -ls
There are screens on:
        2762.pts-0.debian       (Detached)
        2743.pts-0.debian       (Detached)
2 Sockets in /var/run/screen/S-diesel.

Выбираем нужный нам screen, и присоединяемся к нему:

screen -x 2762.pts-0.debian
  • screen — запуск новой сессии;
  • screen -ls — просмотр всех сессий;
  • screen -r <socket_name> — открыть конкретную сессию (если сессия всего одна, то имя можно не указывать).

В самом screen, почти все сочетания клавиш работают через комбинацию [ Ctrl+a ], то есть сначала нажимаете ее, а уже затем управляющую комбинацию, например, что-бы получить исчерпывающую информацию по всем сочетаниям клавиш, жмем: [ Ctrl+a, ? ] ( имейте в виду, комбинация [ Ctrl+a ], относится и к приведенным в списке сочетаниям ).

  • Ctrl + A + D — свернуть сессию. Отсоединить screen от экрана, вернувшись в физическую консоль. Сам screen при этом никуда не девается и висит в фоне, продолжая выполнять ваши задачи.
  • Ctrl + A + K — завершить сессию. Убить окно текущее окно терминала (попросит подтверждения).
  • Ctrl+a, Ctrl+C или с Запустить еще один терминал.
  • Ctrl+a, \ Закрыть screen, все виртуальные терминалы будут при это убиты (попросит подтверждения).
  • F11 - Переключение между виртуальными терминалами, предыдущее окно.
  • F12 - Переключение между виртуальными терминалами, следующее окно.
  • Ctrl+a, A Изменить название окна.
  • [ctrl]+[a] [Shift S] разделяет экран на 2 горизонтальные полосы (верхняя и нижняя).
  • [ctrl]+[a] [TAB] переключаться между ними. Очень полезно для ввода команд в одном "полуокошке", и просмотра логов (tail -f) в другом
  • [ctrl]+[a] [x] Блокировка всего screen. Для разблокировки нужно ввести свой пароль.

Конфигурация screen файл .screenrc

После установки в каталоге /etc (/usr/local/etc) появится общесистемный конфигурационный файл screenrc. Если нужно что-бы разные пользователи, могли запускать программу со своими настройками, просто скопируйте этот файл в домашнюю директорию юзера с именем .screenrc

$ nano ~/.screenrc

# default windows
#

# screen -t local 0
# screen -t mail 1 mutt
# screen -t 40 2 rlogin server

# caption always "%3n %t%? @%u%?%? [%h]%?%=%c"
# hardstatus alwaysignore

hardstatus alwayslastline "%Lw %c" # резервирует последнюю строку экрана под строку статуса

screen -t mc # создает пустое окно с именем указанным после ключа -t
exec /usr/bin/mc # запукает программу в этом окне

screen -t top   
exec /usr/bin/top


select 0 # указывает номер окна, которое будет активным при запуске
  • Типовой конфиг для screen (~/.screenrc):
# Отключаем приветствие
startup_message off
# Включаем utf8
defutf8 on
# Использовать визуальный сигнал (мигание экрана) вместо писка динамика
vbell on
# Размер буфера прокрутки
defscrollback 1000
# Производить отключение сессии при разрыве связи с терминалом
autodetach on
# Открывать Login-шелл
shell -$SHELL
# Активировать возможность прокрутки в xterm (и других эмуляторах терминала)
termcapinfo xterm* ti@:te@
# Волшебная строка
shelltitle '$ |sh'
# Строка состояния
hardstatus alwayslastline "%{+b wk} %c $LOGNAME@%H %=[ %w ] "
  • Рабочий конфиг screenrc
# Отключить вывод лицензионной информации при старте Screen
startup_message off
defscrollback 5000
termcapinfo xterm ti@:te@
termcapinfo xterm-color ti@:te@
hardstatus alwayslastline
hardstatus string '%{gk}[%{G}%H%{g}][%= %{wk}%?%-Lw%?%{=b kR}(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}]%{=b C}[%m/%d/%y %C %A]%{W}'
vbell off
shell -$SHELL
logtstamp on
logtstamp after 1
deflogin on
vbell off
vbell_msg "A6up!  "
 
termcap  sun 'up=^K:AL=\E[%dL:DL=\E[%dM:UP=\E[%dA:DO=\E[%dB:LE=\E[%dD:RI=\E[%dC:IC=\E[%d@:WS=1000\E[8;%d;%dt'
terminfo sun 'up=^K:AL=\E[%p1%dL:DL=\E[%p1%dM:UP=\E[%p1%dA:DO=\E[%p1%dB:LE=\E[%p1%dD:RI=\E[%p1%dC:IC=\E[%p1%d@:WS=\E[8;%p1%d;%p2%dt$<1000>'
 
termcap  hp700 'Z0=\E[?3h:Z1=\E[?3l:hs:ts=\E[62"p\E[0$~\E[2$~\E[1$}:fs=\E[0}\E[61"p:ds=\E[62"p\E[1$~\E[61"p:ic@'
terminfo hp700 'Z0=\E[?3h:Z1=\E[?3l:hs:ts=\E[62"p\E[0$~\E[2$~\E[1$}:fs=\E[0}\E[61"p:ds=\E[62"p\E[1$~\E[61"p:ic@'
 
termcap wy75-42 nx:xo:Z0=\E[?3h\E[31h:Z1=\E[?3l\E[31h
terminfo wy75-42 nx:xo:Z0=\E[?3h\E[31h:Z1=\E[?3l\E[31h
 
termcapinfo xterm-256color ti@:te@
hardstatus string '%{= kG}[ %{G}%H %{g}][ %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)\%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
hardstatus string '%{+b rk}%H%{gk} |%c %{yk}%d.%m.%Y | %{wk} %?%-Lw%?%{bw}%n*%f %t%?(%u)%?%{wk}%?%+Lw%? | %{rk} Load: %l %{wk}'

Пример: top и mc запущены через screen

Например, запустим через screen утилиту top и mc (Midnight Commander). Этот пример одинаково работает, как для Linux, так и BSD систем.

  1. Посмотрим список уже запущенных процессов через screen.
    # screen -ls
    No Sockets found in /tmp/screens/S-root.

    Запущенных программа нет.

  2. Запускаем утилиту top:
    # screen top

    Жмем комбинации клавиш Ctrl+A D и возвращаемся в командную строку. Проверяем список процессов.

    # screen -ls
    There are screens on:
            14178.ttyp0.mail2       (Detached)
    1 Sockets in /tmp/screens/S-root.
  3. Запускаем Midnight Commander
    # screen mc

    Жмем комбинации клавиш Ctrl+A и Ctrl+B. Проверяем список процессов.

    # screen -ls
    There are screens on:
            14178.ttyp0.mail2       (Detached)
            14185.ttyp0.mail2       (Detached)
    2 Sockets in /tmp/screens/S-root.

    Чтобы вернуться к нужному нам процессу, выполним команду с ключем -r и укажем pid процесса. Присоединимся к процессу top:

    # screen -r 14178
Если у нас через screen запущен только один процесс, для переключения на него достаточно выполнить команду "screen -r", указывать pid процесса не нужно.
  1. RTorrent консольный BitTorrent клиент пример запуска в консоли Linux при помощи Screen
  2. Byobu - это расширение для терминального мультиплексора GNU Screen или tmux, используемое с компьютерной операционной системой Linux, которое можно использовать для предоставления экранных уведомлений или состояния, а также управления несколькими окнами с вкладками.

Оригинал mosquito/.screenrc SCREEN SETTINGS

# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# /etc/screenrc
#
# This is the system wide screenrc.
#
# You can use this file to change the default behavior of screen system wide
# or copy it to ~/.screenrc and use it as a starting point for your own
# settings.
#
# Commands in this file are used to set options, bind screen functions to
# keys, redefine terminal capabilities, and to automatically establish one or
# more windows at the beginning of your screen session.
#
# This is not a comprehensive list of options, look at the screen manual for
# details on everything that you can put in this file.
#
#

# ==============================================================================
# SCREEN SETTINGS
# ==============================================================================

# ESCAPE - the COMMAND CHARACTER
# ===============================================================
# escape ^aa  # default
# escape ^^^^ # suggested binding (Control-^) for Emacs users


# PASSWORD
# ===============================================================
# This commands sets the *internal* password for the screen session.
# WARNING!! If this is set then a "lock" command will only let you in to the
# session after you enter the user's account password and then *also*
# the internal password for that session. This gives additional safety but,
# if you forget the internal password then you cannot resume your session.
# Use :password to generate a password
# password ODSJQf.4IJN7E    # "1234"


# VARIABLES
# ===============================================================
# No annoying audible bell, using "visual bell"
# vbell on				# default: off
# vbell_msg "   -- Bell,Bell!! --   "	# default: "Wuff,Wuff!!"

# Automatically detach on hangup.
  autodetach on				# default: on

# Don't display the copyright page
  startup_message off			# default: on

# Uses nethack-style messages
# nethack on				# default: off

# Affects the copying of text regions
  crlf off				# default: off

# Enable/disable multiuser mode. Standard screen operation is singleuser.
# In multiuser mode the commands acladd, aclchg, aclgrp and acldel can be used
# to enable (and disable) other user accessing this screen session.
# Requires suid-root.
  multiuser off

# Change default scrollback value for new windows
  defscrollback 10000			# default: 100

# Define the time that all windows monitored for silence should
# wait before displaying a message. Default 30 seconds.
  silencewait 15			# default: 30

# bufferfile:   The file to use for commands
#               "readbuf" ('<') and  "writebuf" ('>'):
  bufferfile            $HOME/.screen_exchange
#
# hardcopydir:  The directory which contains all hardcopies.
# hardcopydir           ~/.hardcopy
# hardcopydir           ~/.screen
#
# shell:  Default process started in screen's windows.
# Makes it possible to use a different shell inside screen
# than is set as the default login shell.
# If begins with a '-' character, the shell will be started as a login shell.
# shell                 zsh
# shell                 bash
# shell                 ksh
  shell -$SHELL

# shellaka '> |tcsh'
# shelltitle '$ |bash'

# emulate .logout message
  pow_detach_msg "Screen session of \$LOGNAME \$:cr:\$:nl:ended."

# caption always " %w --- %c:%s"
# caption always "%3n %t%? @%u%?%? [%h]%?%=%c"

# advertise hardstatus support to $TERMCAP
termcapinfo  * '' 'hs:ts=\E_:fs=\E\\:ds=\E_\E\\'
termcapinfo xterm* ti@:te@

# set every new windows hardstatus line to somenthing descriptive
# defhstatus "screen: ^En (^Et)"

# don't kill window after the process died
# zombie "^["

# ignore displays that block on output
defnonblock on

# XTERM TWEAKS
# ===============================================================

# xterm understands both im/ic and doesn't have a status line.
# Note: Do not specify im and ic in the real termcap/info file as
# some programs (e.g. vi) will not work anymore.
  termcap  xterm hs@:cs=\E[%i%d;%dr:im=\E[4h:ei=\E[4l
  terminfo xterm hs@:cs=\E[%i%p1%d;%p2%dr:im=\E[4h:ei=\E[4l

# 80/132 column switching must be enabled for ^AW to work
# change init sequence to not switch width
  termcapinfo  xterm Z0=\E[?3h:Z1=\E[?3l:is=\E[r\E[m\E[2J\E[H\E[?7h\E[?1;4;6l

# Make the output buffer large for (fast) xterms.
# termcapinfo xterm* OL=10000
  termcapinfo xterm* OL=100

# tell screen that xterm can switch to dark background and has function
# keys.
  termcapinfo xterm 'VR=\E[?5h:VN=\E[?5l'
  termcapinfo xterm 'k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~'
  termcapinfo xterm 'kh=\EOH:kI=\E[2~:kD=\E[3~:kH=\EOF:kP=\E[5~:kN=\E[6~'

# special xterm hardstatus: use the window title.
  termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

#terminfo xterm 'vb=\E[?5h$<200/>\E[?5l'
  termcapinfo xterm 'vi=\E[?25l:ve=\E[34h\E[?25h:vs=\E[34l'

# emulate part of the 'K' charset
  termcapinfo xterm 'XC=K%,%\E(B,[\304,\\\\\326,]\334,{\344,|\366,}\374,~\337'

# xterm-52 tweaks:
# - uses background color for delete operations
  termcapinfo xterm* be
  termcapinfo xterm* ti@:te@

# Do not use xterm's alternative window buffer, it breaks scrollback (see bug #61195)
  termcapinfo xterm|xterms|xs ti@:te=\E[2J

# WYSE TERMINALS
# ===============================================================

#wyse-75-42 must have flow control (xo = "terminal uses xon/xoff")
#essential to have it here, as this is a slow terminal.
  termcapinfo wy75-42 xo:hs@

# New termcap sequences for cursor application mode.
  termcapinfo wy* CS=\E[?1h:CE=\E[?1l:vi=\E[?25l:ve=\E[?25h:VR=\E[?5h:VN=\E[?5l:cb=\E[1K:CD=\E[1J


# OTHER TERMINALS
# ===============================================================

# make hp700 termcap/info better
  termcapinfo  hp700 'Z0=\E[?3h:Z1=\E[?3l:hs:ts=\E[62"p\E[0$~\E[2$~\E[1$}:fs=\E[0}\E[61"p:ds=\E[62"p\E[1$~\E[61"p:ic@'

# Extend the vt100 desciption by some sequences.
  termcap  vt100* ms:AL=\E[%dL:DL=\E[%dM:UP=\E[%dA:DO=\E[%dB:LE=\E[%dD:RI=\E[%dC
  terminfo vt100* ms:AL=\E[%p1%dL:DL=\E[%p1%dM:UP=\E[%p1%dA:DO=\E[%p1%dB:LE=\E[%p1%dD:RI=\E[%p1%dC
  termcapinfo linux C8
# old rxvt versions also need this
# termcapinfo rxvt C8


# KEYBINDINGS
# ==============================================================
# The "bind" command assign keys to (internal) commands
# SCREEN checks all the keys you type; you type the key
# which is known as the "command character" then SCREEN
# eats this key, too, and checks whether this key is
# "bound" to a command.  If so then SCREEN will execute it.
#
# The command "bind" allows you to chose which keys
# will be assigned to the commands.
#
# Some commands are bound to several keys -
# usually to both some letter and its corresponding
# control key combination, eg the command
# "(create) screen" is bound to both 'c' and '^C'.
#
# The following list shows the default bindings:
#
# break       ^B b
# clear       C
# colon       :
# copy        ^[ [
# detach      ^D d
# digraph     ^V
# displays    *
# dumptermcap .
# fit         F
# flow        ^F f
# focus       ^I
# hardcopy    h
# help        ?
# history     { }
# info        i
# kill        K k
# lastmsg     ^M m
# license     ,
# log         H
# login       L
# meta        x
# monitor     M
# next        ^@ ^N sp n
# number      N
# only        Q
# other       ^X
# pow_break   B
# pow_detach  D
# prev        ^H ^P p ^?
# quit        \
# readbuf     <
# redisplay   ^L l
# remove      X
# removebuf   =
# reset       Z
# screen      ^C c
# select      " '
# silence     _
# split       S
# suspend     ^Z z
# time        ^T t
# title       A
# vbell       ^G
# version     v
# width       W
# windows     ^W w
# wrap        ^R r
# writebuf    >
# xoff        ^S s
# xon         ^Q q
# ^]  paste .
# -   select -
# 0   select 0
# 1   select 1
# 2   select 2
# 3   select 3
# 4   select 4
# 5   select 5
# 6   select 6
# 7   select 7
# 8   select 8
# 9   select 9
# I   login on
# O   login off
# ]   paste .
#

# And here are the default bind commands if you need them:
#
# bind A title
# bind C clear
# bind D pow_detach
# bind F fit
# bind H log
# bind I login on
# bind K kill
# bind L login
# bind M monitor
# bind N number
# bind O login off
# bind Q only
# bind S split
# bind W width
# bind X remove
# bind Z reset

# Let's remove some dangerous key bindings ...
  bind k
  bind ^k
# bind .  dumptermcap # default
  bind .
# bind ^\ quit        # default
  bind ^\
# bind \\ quit        # default
  bind \\
# bind ^h ???         # default
  bind ^h
# bind h  hardcopy    # default
  bind h

  bindkey "" next
  bindkey "" prev
  bindkey "" screen

# ... and make them better.
  bind 'K' kill
  bind 'I' login on
  bind 'O' login off
  bind '}' history

# Yet another hack:
# Prepend/append register [/] to the paste if ^a^] is pressed.
# This lets me have autoindent mode in vi.
  register [ "\033:se noai\015a"
  register ] "\033:se ai\015a"
  bind ^] paste [.]


# hardstatus alwaysignore
# hardstatus alwayslastline "%Lw"

# Resize the current region. The space will be removed from or added to
# the region below or if there's not enough space from the region above.
  bind = resize =
  bind + resize +3
  bind - resize -3
# bind _ resize max
#
# attrcolor u "-u b"
# attrcolor b "R"

# STARTUP SCREENS
# ===============================================================
# Defines the time screen delays a new message when one message
# is currently displayed. The default is 1 second.
#  msgminwait 2

# Time a message is displayed if screen is not disturbed by
# other activity. The dafault is 5 seconds:
#  msgwait 2

# Briefly show the version number of this starting
# screen session - but only for *one* second:
#  msgwait 1
#  version

# Welcome the user:
# echo "welcome :-)"
# echo "I love you today."

# Uncomment one/some following lines to automatically let
# SCREEN start some programs in the given window numbers:
# screen -t MAIL        0 mutt
# screen -t EDIT        1 vim
# screen -t GOOGLE      2 links http://www.google.com
# screen -t NEWS        3 slrn
# screen -t WWW         4 links http://www.math.fu-berlin.de/~guckes/
# screen 5
# screen 6

# Set the environment variable var to value string. If only var is specified,
# you'll be prompted to enter a value. If no parameters are specified,
# you'll be prompted for both variable and value. The environment is
# inherited by all subsequently forked shells.
# setenv PROMPT_COMMAND 'echo -n -e "\033k\033\134"'

# Don't you want to start programs which need a DISPLAY ?
# setenv DISPLAY ''

hardstatus alwayslastline
hardstatus string '%{WB}%H %{W}| %{Y}%l %{W}| %?%{w}%-w%?%{+b g}[%{W}%{r}%n%{W} %t%?{%u}%?%{-b g}]%{W}%?%+w%?%=%{G}| %{Y}%d-%m-%Y %c:%s '
PQ VPS сервера в 28+ странах.