Launching and operating basics Screen. Configuring the Linux screen utility Switching between screen

Screen is a very handy program if you often work in the console by connecting to the server via SSH. In this case screen allows you to keep several different screens in one console, conveniently switching between them.

screen installation

To install on you need to run the command

$ sudo apt-get installscreen

After that, you need to run screen:

$screen

Upon launch, you will either see a prompt command line, or the "welcome screen" by pressing SPACE or ENTER in which you will also be taken to the shell. In this case, all commands will be launched already “inside” the screen.

If you need to listen to the log file constantly and display them on the screen. Let's create a new screen console like this:

Screen -d -m -S tail-log tail -f /var/log/nginx/error.log

In order to exit the screen (exit, not disconnect), it is enough to exit all shells open in it, or, alternatively, press the key combination Ctrl+A, and answer "y" to the question "do you really want to exit".

You can disconnect from the current screen without closing or ending the session by using the combination Ctrl+A then d.

Connecting to screen

If there is already a screen session running on the machine and you want to connect to it, then

  1. if there is only one session, just the command is enough: Ctrl+A then x
  2. if there are several sessions, then: you can see the list of running screens with the screen -ls command: $ screen -ls There are screens on: 2762.pts-0.debian (Detached) 2743.pts-0.debian (Detached) 2 Sockets in /var/run/screen/S-diesel.

    We select the screen we need, and join it:

    Screen -x 2762.pts-0.debian

    The names of the sessions by default are not very informative - just process numbers, if you want to run and use several screen sessions for different tasks, you can come up with more sane names. We come up with a name, then we start a new screen like this:

    $ screen -S "job1"

    where job1 is the name of our "named" session. Now in -ls we will see much clearer:

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

    and we can connect to the job1 session by simply specifying its name:

    $ screen -x job1

Switching between screens

As you already understood, sessions are saved even when you are disconnected from the server, and everything that is running in them continues to be executed. However, the possibilities of screen are not limited to this. Sometimes, when working via ssh, I really want to have several terminals open at once. Screen can help with this too.

Ctrl+A then?- display all available commands and parameters, to exit space or Enter
Ctrl+A then c- create create a screen window
Ctrl+A then p- strongvios switch to previous screen window
Ctrl+A then n- next switch to the next screen window
Ctrl+A then d- deadached disconnect from screen leaving the session running
Ctrl+A then K- kill kill the screen session
Ctrl+A then x- screen lock, to unlock you need to enter your Linux password.
Ctrl+A then w- list of windows running in the screen
Ctrl+A then A- sign the name of different windows in the screen
Ctrl+A then |- split windows vertically, after Ctrl+A vertical bar (not small L) or type split -v
Ctrl+A then S- split windows horizontally
Ctrl+A then Tab- transition between halves
Ctrl+A Q- hide all windows and leave only the current one
Ctrl+D- exit the screen session

Copying text between windows and scrolling the screen

Ctrl+A then [- switches the screen to Copy mode, while you can move around the window with the cursor keys, to determine the beginning of the text fragment, press the spacebar. Move the cursor with the arrows to mark the end of the selection and press the spacebar again. Everything, the fragment is copied. It is inserted into the same or another window by pressing ] .

To exit this mode, simply press Esc.

Command execution monitoring

If you have multiple windows open, it can be useful to monitor activity in one of them. For example, if you have run a command and are interested in knowing when it will complete. Go to the window with this command, click

Ctrl+A then m- and enable window activity monitoring. Now, if the text of the window changes, screen will beep, the “@” symbol will be displayed next to its name in the list of windows, and the inscription “Activity in window #…” will appear at the bottom of the window for a few seconds!

Logging and print screen

It is also possible to trace all text output to the window to a special screenlog file. where N is the window number.

Ctrl+A then H

Now all the results of your work will be logged and you can see the history of not only input, but also output.

Also the command to remove the current window

Ctrl+A then h

transfers to a hardcopy file. only the contents of the current window, rather than constantly monitoring I/O. Analog print screen.

screen configuration

Screen is configured using the ~/.screenrc file in your home directory. For example, you can write there:

Caption always "%(= 45)%(+b w)Screen: %n | %h %=%t %c" hardstatus alwayslastline "%-Lw%(= BW)%50>%n%f* %t%( -)%+Lw%<"

This will give you an all-time visible list of open windows, and other useful information at the bottom of the screen.

Also, when Screen is launched, by default, commands from the /etc/screenrc file and the .screenrc file from the user's home directory are executed. Many of the commands that can be used in configuration files have been discussed in the tables above in the "Text command" field. Below is an example Screen configuration from a .screenrc file:

# Disable displaying license information at startup Screen startup_message off # Open a shell to run chdir screen -t Work # Open a shell to manage configuration chdir /etc screen -t Configuration # Open a shell to view logs chdir /var/log screen -t Logs # Select first window after start select 0

This fairly simple configuration file opens three windows named Work, Configuration, Logs, respectively in the user's home directory, in the /etc directory, and in the /var/log directory. After launch, the first window named Work will be on the screen. Figure 1 shows an example of how Screen works with this configuration file.

Completion Screen

If for some reason the screen session stops responding, you can end it. To do this, you need to connect to the desired session, then press Ctrl + A and type ":quit".

For over a year, a classmate urged me to start using screen while I was at work. As time went on, I still did not reach this hand. And in vain ...

Screen is a full-screen window manager that allows you to work with several windows at once from one terminal session. In other words, by connecting to a remote machine via ssh, you can work with several of its windows at once within one session, like pressing ctrl + alt + f1, ctrl + alt + f2 ... The program has one important feature: when the connection is broken or disconnected from the "screen" processes running in it do not stop and you can return to them at any time. You can find many applications for this. For example, when working with a remote server, you can be sure that the running compilation of the program will reach the end, even if the connection suddenly breaks.

Installation

The program is installed as usual, for example, in Ubuntu like this:

apt-get install screen

It is also worth noting that in my Ubuntu 10.04 Desktop this package was already on the system.

Using Screen

Starting the program:

After starting the program, brief information about it will be shown. In order to remove it, press enter or space. You might think that nothing has happened, however, this is not the case: you are inside a terminal running in screen. After that, we see the console familiar to us.

To issue screen commands, use the ctrl+a key combination. The following is a list of the main commands used to control the shell:

  • c - create a new window
  • n - switch to the next window
  • p - switch to the previous window
  • k - close the window
  • d - disconnect from screen
  • » - list of windows running in the screen

For example, to create a new window, press ctrl+a and press the c key.

When disconnected from the screen, all windows and processes running in them continue to run. To view running screenshots, use the command:

The output of the command will be something like this:

There are screens on:
18388.pts-4.asid-ubuntu-vm (9/23/2010 05:05:30) (Detached)
18272.pts-0.asid-ubuntu-vm (9/23/2010 04:54:23) (Detached)
2 sockets in /var/run/screen/S-root.

In order to connect to a running screen, run the command screen -r . For example, like this:

screen -r 18388.pts-4.asid-ubuntu-vm

or shorter:

If you forgot to disconnect from a screen from another machine (the output of screen -ls will show atached next to it), you can intercept it using the -rd option:

screen -rd 18388

To connect to the last screen, you can simply:

Well, that seems to be all. More about these and other commands, as usual, in the mana.

Screen is a console application that allows you to use multiple terminal sessions in one window. The program acts within a shell session and acts as a container and manager for other terminal sessions, much like a window manager manages windows.

In many cases, creating multiple terminal windows is not possible. You may need to manage multiple console sessions without starting the X server; you may need to quickly access several remote cloud servers or display a running program while working on some other task. All of these needs are easily met with the Screen console.

Installation

This guide uses Ubuntu 12.04; on every modern distribution, all commands except for installation will be the same.

To install the console on Ubuntu, use the "apt-get" command:

sudo apt-get update
sudo apt-get installscreen

Usage Basics

To start a screen session, simply call the "screen" command:

screen
Screen version 4.00.03jw4 (FAU) 2-May-06
Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program (see the file COPYING); if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to
[email protected]

Upon launch, the licensing page will appear. Press "Return" or "Enter" to continue.

Further actions of the program may be unexpected. A regular command line is provided and nothing seems to happen. Maybe there was a screen error? To find out, use the keyboard shortcut. Press and hold the control key (Ctrl), press "a", then the "v" key:

This action queries Screen for version information; the displayed result indicates that the console is working.

Now you need to familiarize yourself with the management of the console. Screen is controlled primarily by keyboard shortcuts. Each such combination must be preceded by "Ctrl-a" (hold down the "Ctrl" key before pressing "a"). This sequence of keystrokes tells Screen to pay attention to the next keys pressed.

This principle has already been used when requesting information about Screen. Use it again for some more useful information.

Ctrl-a ?
Screen key bindings, page 1 of 2.
Command key: ^A Literal ^A: a
break ^B b license , removebuf =
clear C lockscreen ^X x reset Z
colon: log H screen ^C c
copy ^[ [ login L select "
detach ^D d meta a silence _
digraph ^V monitor M split S
displays * next ^@ ^N sp n suspend ^Z z
dumptermcap . number N time ^T t
fit F only Q title A
flow ^F f other ^A vbell ^G
focus ^I pow_break B version v
hardcopy h pow_detach D width W
help? prev ^H ^P p ^? windows ^W w
history ( ) quit \ wrap ^R r
info i readbuf< writebuf >
kill K k redisplay ^L l xoff ^S s
lastmsg ^M m remove X xon ^Q q

This is a list of internal hotkeys. It is worth remembering the command to call it, as this is a great way to quickly get help. As you can see at the bottom of the file, to get more commands, you need to press the space bar.

Now call the "top" command, which provides information about the processes.

top
top - 16:08:07 up 1:44, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 58 total, 1 running, 57 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 507620k total, 262920k used, 244700k free, 8720k buffers
Swap: 0k total, 0k used, 0k free, 224584k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 3384 1836 1288 S 0.0 0.4 0:00.70 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/0
5 root 20 0 0 0 0 S 0.0 0.0 0:00.12 kworker/u:0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
7 root RT 0 0 0 0 S 0.0 0.0 0:00.07 watchdog/0
8 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 cpuset
9 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
11 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
12 root 20 0 0 0 0 S 0.0 0.0 0:00.03 sync_supers
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default
14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md

Now you can view the processes on the VPS. But what commands to use to get more detailed information about running programs? You can not exit the "top" command, but simply create a new window to run these commands.

The "Ctrl-a c" sequence creates a new window. Now you can run any command without disturbing the commands running in another window (in this case, process monitoring).

Where did that other window go? You can return to it with the command:

This sequence displays the next running window. The list of windows is circular, so if there are no other windows behind the current window, it returns the first window.

This sequence switches windows to reverse order. If, for example, there are three windows running and the third one is current, this command will display the second window.

When constantly switching between two windows, it is useful to use the following keyboard shortcut:

This sequence takes the user to the most frequently visited window. In the previous example, it would bring up the third window again.

Now, keeping track of all the windows that have been created may seem impossible. Fortunately, the Screen console provides several ways to manage different sessions. To start, create three new windows (total of 4 windows), and then use one of the easiest window management tools - "Ctrl-a w".

Ctrl-a c
Ctrl-a c
Ctrl-a c
Ctrl-aw
0$bash 1$bash 2-$bash 3*$bash

This command provides useful information - a list of open windows. In this case, 4 windows are open. Each window has a number starting from 0. An asterisk next to the number indicates the current window.

As you can see, at the moment window #3 is the current window (in fact, it is the fourth one, since the numbering starts from 0). How to quickly switch to window #1?

The sequence number can be used to jump directly to the desired window. Open the window list again:

Ctrl-aw
0$bash 1*$bash 2$bash 3-$bash

As you can see, window #1 is now the current one. Try another way to switch between windows.

Ctrl-a"
Num Name Flags
bash $0
1 bash$
bash $2
3 bash$

This time the navigation menu is displayed. Now you can navigate using the up and down arrows or the "j" and "k" keys, as is done in the vi text editor. You can switch to the selected window by pressing the "Return" or "Enter" button.

This is useful enough, but at the moment all windows are called "bash". This is not very convenient. Now we need to name the session. Switch to the window you want to name and use the "Ctrl-a A" sequence.

Ctrl-a 0
Ctrl-A
Set window "s title to: bash

Using this sequence, you can give the session a name. Use the backspace key to erase "bash" and enter any new name. For example, name window #0 "monitoring" and run the command "top" in it.

Set window "s title to: monitoring
Ctrl-a"
Num Name Flags
0 monitoring$
1 bash$
bash $2
3 bash$

Window #0 has a more convenient name.

Now you can create and rename windows; but how to get rid of the unnecessary window?

Use the sequence "Ctrl-a k", which means "kill":

Ctrl-a k
Really kill this window

Screen session management

To exit Screen and close all windows, use the following sequence of commands:

Ctrl-a \
Really quit and kill all your windows

This will end the Screen session. All created windows, as well as any work in progress, will be lost.

Using Screen has one huge advantage. The session can not be deleted, but simply removed. Removing a session allows programs running in Screen to continue working, and also gives access to the main console session (the one that starts working with Screen). This Screen session still exists, it's just managed in the background.

Ctrl-a d

So the session is over. How to return to this session?

The "-r" flag means "reattach", or "restore". But what if there are multiple Screen sessions? What if a Screen session was created and dropped, and then a new session was created that was also removed?

screen
Ctrl-a d
screen
Ctrl-a d

How to tell Screen which session to restore?

screen -ls
There are screens on:
2171.pts-0.Blank (07/01/2013 05:00:39 PM) (Detached)
1835.pts-0.Blank (07/01/2013 03:50:43 PM) (Detached)
2 sockets in /var/run/screen/S-justin.

The list of sessions is now displayed. To restore the second session, enter its identification number after the "-r" flag.

screen –r 1835

What if you need to restore the session on two remote computers or terminal windows? Use the "-x" flag to split the session.

Terminal management in Screen

There are a number of commands that allow you to manage terminal sessions running in Screen.

To copy text, use the sequence:

This sequence provides a cursor that can be moved using the arrow keys or the "h", "j", "k", "l" keys (as in vi). Move the cursor to the beginning of the piece of text you want to copy and press "Return" or "Enter". Then move the cursor to the end of the fragment and press "Return" or "Enter" again. The text will be copied to the clipboard.

Note that this is also the console's scrolling mechanism. If you need to view text that is offscreen, type "Ctrl-a [" and scroll up.

You can paste copied text with a closing square bracket

You may also want to view programs running in another screen window.

For example, some data is compiled in one window and you need to know when the procedure is completed. You can tell Screen to monitor the inactivity of this window with the "Ctrl-a _" sequence, which will tell you if no output is displayed within 30 seconds.

Let's take a simpler example. Tell the Screen console to tell you when the window has finished pinging Google 4 times.

ping -c 4 www.google.com
Ctrl-a_
The window is now being monitored for 30 sec. silence.

Now you can work in another window and wait for the task completion notification in the previous window.

Ctrl-a 1
Window 2: silence for 30 seconds

You can also, conversely, request notification of the activity of a particular window. This is done using the sequence "Ctrl-a M".

sleep 20 && echo “output”
Ctrl-a M
Window 2 (bash) is now being monitored for all activity.

A notification will now appear when data is displayed in this window.

Ctrl-a 1
activity in window 2

For example, you need to make a number of important changes, and in doing so, you need to get a log of running commands. The session can be recorded using:

Ctrl-a H
Creating logfile "screenlog.1".

Screen areas

If you need to see several windows at once, you can use the so-called "areas". New regions are created when the current region is split. To split the current area horizontally, you can type:

This will move the current window to the top of the screen and open a new empty area below it. To move to the bottom area, use "tab":

You can now create a new window in the lower area, or switch to another window in the normal way.

If necessary, destroy the current area, use the following sequence:

This removes the region without destroying the current window. That is, if a command was launched in this area, then it is still possible to access it as a normal window, just a separate area is destroyed.

If you need to make a vertical section, you can use the sequence:

Note: This is not a 1 (ones) or L character. It is a pipe character that is on the same key as the "\" character on most keyboards. The controls for vertical areas are the same as those for horizontal areas. If you need to delete several different areas and return to one area, you can use this sequence, which destroys all areas except the current one:

Using Byobu

A big improvement for Screen is a program called "byobu". It acts as a wrapper for Screen and provides an improved usability. On Ubuntu it can be installed with:

sudo apt-get install byobu

Before we start, we need to tell byobu to use screen as an output buffer. This can be done with the command:

byobu-select-backend
Select the byobu backend:
1.tmux
2. screen
Choose 1-2 :

Here you can set screen as the default terminal manager.

Now you can type "byobu" instead of "screen" to start a session.

byobu
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-24-virtual i686)
* Documentation: https://help.ubuntu.com/
Welcome to the light, powerful, text window manager, Byobu.
You can toggle the launch of Byobu at login with:
"byobu-disable" and "byobu-enable"
For tips, tricks, and more information, see:
*http://bit.ly/byobu-help
[email protected]:~$
u? 12.04 0:* 118! 3h55m 2.00 2.0GHz 496M6% 2013-07-01 18:19:21

As you can see, screen is now wrapped in a user-friendly interface.

By typing "Ctrl-a" for the first time, you need to tell byobu to recognize this command as a screen command, not Emacs.

Ctrl-a
Configure Byobu"s ctrl-a behavior...
When you press ctrl-a in Byobu, do you want it to operate in:
(1) Screen mode (GNU Screen's default escape sequence)
(2) Emacs mode (go to beginning of line)
Note that:
- F12 also operates as an escape in Byobu
- You can press F9 and choose your escape character
- You can run "byobu-ctrl-a" at any time to change your selection
Select :

Select 1 to use byobu normally.

The interface provides a lot of useful information, such as a list of windows and system information. On Ubuntu, it even reports how many packages have security updates as a number followed by an exclamation mark on a red background.

The only difference between byobu and screen is that byobu manages sessions. If you simply type "byobu" again after disconnecting, it will restore the previous session instead of creating a new one.

To create a new session, you need to type:

byobu -S sessionname

Change "sessionname" (session name) to your liking to invoke a new session. The list of current sessions can be viewed with:

byobu –ls
There are screens on:
22961.new (07/01/2013 06:42:52 PM) (Detached)
22281.byobu (07/01/2013 06:37:18 PM) (Detached)
2 sockets in /var/run/screen/S-root.

If there are multiple sessions, then after entering "byobu" a menu will appear in which you can select which session you want to connect to.

byobu
Byobu sessions...
1.screen: 22961.new (07/01/2013 06:42:52 PM) (Detached)
2.screen: 22281.byobu (07/01/2013 06:37:18 PM) (Detached)
3. Create a new Byobu session (screen)
4. Run a shell without Byobu (/bin/bash)
Choose 1-4 :

You can select any of the existing sessions, create a new byobu session, or even a new shell that doesn't use byobu.

One of the very useful features for a remote cloud server is the ability to run byobu automatically every time you connect to a session. This means that when a session is disconnected, work will not be lost, and it will be easy to reconnect to this session again.

To enable byobu to start automatically every time you log in, type in the terminal:

byobu-enable
The Byobu window manager will be launched automatically at each text login.
To disable this behavior later, just run:
byobu-disable
Press to continue...

As stated above, to disable this feature, simply type:

Then it will not start automatically.

Tags: ,

Screen is a very useful command that offers the ability to use multiple shell windows (sessions) from a single . When a session is disconnected or there is a network disruption, the process that runs in the screen session will still run, and you can reconnect to the screen session at any time. It's also very handy if you want to run a lengthy process all the time, or connect to a session shell from multiple locations.

In this article, we'll show you the basics of installing and using a screen on .

How to install the screen

Screen comes preinstalled on some of the popular distributions. You can check whether it is installed or not on the server with the following command

Screen -v Screen version 4.00.03 (FAU)

If you don't have a screen to VPS, you can easily install it using the package manager provided with operating system.

/RedHat/Fedora

yum -y install screen

/

apt-get -y install screen

How to start a screen session

You can start a screen by typing the word "screen" at the command line and a new screen session will be launched which looks the same as on the command line

It's a good practice to start screen occupations with descriptive names so you can easily remember what process is running in the session. To create a new session with the session name run the following command

Screen -S name

and replace ' name‘ with a friendly name for your session.

Detach screen from session

To disconnect from the current screen session you can press the ‘ Ctrl-A' And ' D' on keyboard. All screen sessions will still be active and you can reconnect to them at any time later.

Revisit the session screen

If you are disconnected from your session or your connection drops for some reason, you can easily reconnect by running the following command:

Screen-r

If you have multiple screen sessions you can list them with ‘ ‘

Screen -ls There are screens on: 7880.session (Detached) 7934.session2 (Detached) 7907.session1 (Detached) 3 Sockets in /var/run/screen/S-root.

In our example, we have three active screen sessions. So, if you want to restore the "session2" session, you can do

Screen -r 7934

or you can use screen name

Screen -r -S session2

End screen session

There are several ways to end a screen session. You can do this by clicking ‘ ctrl‘ + ‘ D‘ by keyboard or command line command ‘ exit‘.

To see all the useful properties of the command screen, you can check man page screen.

Man screen NAME screen - screen manager with VT100/ANSI terminal emulation SYNOPSIS screen [ -options ] [ cmd [ args ] ] screen -r ] screen -r sessionowner/]

You are logged into your remote server via ssh, happily tapping on the keyboard, doing some kind of admin business and oops! The characters stopped printing and the same message fell out, which, at times, causes an irresistible desire to break the keyboard. connection closed. Common situation? Yes, yes, your session just crashed and you will have to do everything again ... This can be avoided by using screen. It will not only allow you to keep your session safe and sound, but also allow you to keep several sessions open in one terminal window.

What is screen?
First, let's look at the man page: "Screen is a full-screen window manager that allows you to share a physical terminal among several independent processes (usually interactive shells)".
screen has several features that can greatly help when performing tasks on remote servers via ssh. I will talk about the three features I use most often: multi-window, logging and sessions. You will have to refer to the man page for more details.

screen installation
Most likely, screen is already on your system. You can check this with the command

If which fails, install screen with package manager your system. My servers are mostly CentOS and Debian, so I set up screen like this:

# yum install screen (for CentOS)
# apt-get install screen (for Debian)

FreeBSD also comes across, in this case I use ports:

# cd /usr/ports/sysutils/screen
make install clean

Using screen
Screen is launched from the command line just like any application :)

You may or may not get a message that screen is running... Depends on your system. If you don't receive a message, then you might think that nothing happened. However, it is not. You already inside a terminal running in screen. This is a normal fully functional shell, except for a few special commands. Screen uses the Ctrl+A keyboard shortcut to issue commands to terminals within itself. Try pressing Ctrl+A and then?
You will see something like this:

Screen key bindings, page 1 of 2.

Command key: ^A Literal ^A: a

break ^B b lockscreen ^X x reset Z
clear C log H screen ^C c
colon:login L select"'
copy ^[ [ meta a silence _
detach ^D d monitor M split S
digraph ^V next ^@ ^N sp n suspend ^Z z
displays * number N time ^T t
fit F only Q title A
flow ^F f other ^A vbell ^G
focus ^I pow_break B version v
help? pow_detach D width W
history ( ) prev ^P p ^? windows ^W w
info i readbuf< wrap ^R r
kill K redisplay ^L l writebuf >
lastmsg ^M m remove X xoff ^S s
license , removebuf = xon ^Q q

Screen accepts command keyboard shortcuts after pressing Ctrl+A. You can change this behavior with $HOME/.screenrc config file

Multiwindow
Screen like most window managers, supports multiple windows. This is very handy for running parallel tasks without opening new ssh sessions. For example, I have four or five sessions open all the time with several tasks in each. Previously, I would have to open about 15 terminals, logins, sessions .. Tiresome, isn't it? These inconveniences are completely solved by screen. Now I can quite manage with one terminal.

A new window is opened by the key combination “Ctrl+a c”. After clicking, you will see a new terminal with your prompt in the same window. At the same time, the previous windows also continue to work. Let's try: run screen and top in it

Mem: 506028K av, 500596K used, 5432K free,
0K shrd, 11752K buff
393660K cached


6538 root 25 0 1892 1892 596 R 49.1 0.3
6614 root 16 0 1544 1544 668 S 28.3 0.3
7198 admin 15 0 1108 1104 828 R 5.6 0.2

Now open a new window by pressing “Ctrl+a c”

Go back by pressing Ctrl+a n

Mem: 506028K av, 500588K used, 5440K free,
0K shrd, 11960K buff
Swap: 1020116K av, 53320K used, 966796K free
392220K cached

PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
6538 root 25 0 1892 1892 596 R 48.3 0.3
6614 root 15 0 1544 1544 668 S 30.7 0.3

top remains the same. You can create multiple windows and switch between them using Ctrl+a n to switch to the next window and Ctrl+a p to switch to the previous one. In this case, each running process will remain in a working state.

Disconnecting from screen
There are two ways to log out of screen: the first is to simply log out. You can use the keyboard shortcut Ctrl+a K or just type exit. This method will “kill” the current window if you have more than one or stop screen altogether.
The second way is to detachment. This method leaves the current process running and simply disconnects you from the terminal. For example, if you start some very long process through an ssh session that does not require your attention, then you can simply disconnect from screen with Ctrl+a d. This will take you back to your original shell. All processes started in screen remain running and you can connect to them later.

Joining a session
You are compiling a large program in remote server using screen. And of course, according to Murphy's laws, the connection is terminated for reasons beyond your control. Don't panic, screen saved everything :) Just connect to the server again and look at the processes running under screen with

# screen -ls
There are screens on:
31619.ttyp2.server (Detached)
4731.ttyp2.server (Detached)
2 sockets in /tmp/screens/S-root.

In this example, two screen sessions are running. To connect to the desired one, use the command

#screen -r 31619.ttyp2.server

Just use screen with the r flag and the session name to reconnect. It is very comfortable. You can, for example, start some long process at work and, returning home, continue monitoring its implementation.

Logging
It seems to me very important sometimes to save a complete log of your actions. Luckily, screen handles this easily. Just activate logging by pressing Ctrl+a H. Screen will continue logging throughout the entire process. It can be very useful to go back and look at the order of the necessary actions.

And a bit more…
Screen can monitor window activity. Whether you're downloading something large, compiling a program, or just running a lengthy process, you may end up with an empty terminal for a long time with no sign of activity. And the process, meanwhile, continues to run. Or, on the contrary, look at the flow of debugging information, waiting for the end of the process. To start monitoring, go to the terminal you want to monitor and press Ctrl+a M to monitor activity (triggered when there is new information) or Ctrl+a _ to monitor inactivity (triggered when the terminal stops receiving information). You can then safely switch to another window or create a new one. When an event occurs, screen will warn you about it with a message with the window number in the terminal title. To quickly switch to this window, use Ctrl+a ” (this is the quote character). You will see a list of all currently active windows. You can use the arrows to navigate to the desired window or simply dial the number of the desired window. To stop monitoring, go to the required window and cancel monitoring with the same command. For example, to stop monitoring activity, press Ctrl+a M

mob_info