Setting up the screen utility in Linux. Using screen in Linux How to close screen in putty

S creen is a very useful command that offers the ability to use multiple shell windows (sessions) from one . When a session is disconnected or there are network disruptions, the process that is running in the screen session will still run, and you can reconnect to the screen session at any time. This is also very convenient if you want to run a long-running process continuously or connect to shell sessions from multiple locations.

In this article we will show you the basics of installing and using the screen on .

How to install the screen

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

Screen -v Screen version 4.00.03 (FAU)

If you don't have a VPS screen, 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 that looks the same as the command line

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

Screen -S name

and replace ' name‘ with a clear title for your session.

Detach screen from session

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

Return to session screen

If you are disconnected from your session or your connection is interrupted 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 session "session2" 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' on the keyboard or using the command command lineexit‘.

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

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

For over a year, a classmate encouraged me to start using screen while working. As time passed, I still couldn’t get around to it. But in vain...

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

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 in the system.

Using Screen

Launching the program:

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

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

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

For example, to create a new window you need to 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 screens, use the command:

The output of the command will be something like this:

There are screens on:
18388.pts-4.asid-ubuntu-vm (23.09.2010 05:05:30) (Detached)
18272.pts-0.asid-ubuntu-vm (23.09.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 (in the output of screen -ls there will be an atached message next to it), you can intercept it using the -rd parameter:

screen -rd 18388

To connect to the last screen you can simply:

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

You logged into your remote server via ssh, happily tap on the keyboard, doing some admin work and oops! The characters stopped being printed and the same message fell out, which sometimes 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 if you use screen. It will not only keep your session safe and sound, but will 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 distinctive features that can be of great help when performing tasks on remote servers via ssh. I will talk about the three most frequently used features by me: multi-windowing, logging and sessions. You will have to refer to the man page for more details.

Installing screen

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

If which does not give results, then install screen using package manager your system. My servers are mostly CentOS and Debian, so I set screen like this:

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

FreeBSD also comes across, in this case I use the 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 receive a message that screen is running... Depends on your system. If you don't receive a message, you may think nothing happened. However, it is not. You already inside a terminal running in screen. This is a normal, fully functional shell, with the exception of a few special commands. Screen uses the keyboard shortcut Ctrl+A 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 using the $HOME/.screenrc config file

Multi-window

Screen, like most window managers, supports multiple windows. This is very convenient for performing parallel tasks without opening new ssh sessions. For example, I always have four or five sessions open with several tasks in each. Previously, I would have had to open about 15 terminals, logins, sessions... Tiring, isn't it? These inconveniences are completely solved by screen. Now I can easily get by with just one terminal.

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

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 remained in the same state. 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 working condition.

Disconnect from screen

There are two ways to disconnect from 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 several of them or stop screen altogether.
The second way is to disconnection. This method leaves the current process running and simply disconnects you from the terminal. For example, if you are running some very long process through an ssh session that does not require your attention, then you can simply disconnect from screen using 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.

Connecting to a session

You compile big program on 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 using

# 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, run some lengthy process at work and, upon returning home, continue monitoring its execution.

Logging

It seems to me that it is very important sometimes to keep a complete log of your actions. Luckily, screen handles this easily. Simply activate logging by pressing Ctrl+a H. Screen will continue logging throughout the entire process. It can be very useful to go back and see the order of the necessary actions.

And a bit more…

Screen can monitor window activity. If you're downloading something large, compiling a program, or just going through a long process, you can for a long time observe an empty terminal with no signs of activity. Meanwhile, the process continues to run. Or, conversely, look at the stream of debugging information, waiting for the end of the process. To start tracking, go to the terminal that you want to monitor and press Ctrl+a M to track activity (will work when new information appears) or Ctrl+a _ to track inactivity (will work when information stops flowing into the terminal). Then you can safely switch to another window or create a new one. When an event occurs, screen will alert you 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 active windows on this moment. To go to the boring one, you can use the arrows or simply dial the number of the desired window. To stop monitoring, go to the desired window and cancel monitoring using the same command. For example, to stop monitoring activity, press Ctrl+a M

5,958 views

The creators of Screen call the utility a (pseudo) terminal manager. The idea is that instead of several terminals open for simultaneous work with different user programs, for example via ssh, to provide access to these terminals from one single terminal, as if we were working in a “real” TTY and switching from one to another with the Alt keys. Fn.

In fact, Screen can do much more. I will list its two main advantages.


Rice. 2. Both pseudo-terminals in one Screen window (above) instead of two separately open windows(at the bottom).

Before moving on to the description of the utility, it will be useful to agree on terminology, following which simplifies the understanding of the operating principle of Screen.

1. Terms and definitions

Window— a pseudo-terminal with its own bash (nonlogin). Windows are numbered from 0 to 9 (window numbers can be redefined). They can (and should) be given names.

Region— a rectangular area of ​​the screen in which one of the windows is displayed. The screen can be divided into areas both vertically and horizontally.

Screen layout (layout). This is a set of areas into which the terminal screen is divided. Each layout can be given a name and added to the configuration file so that you can quickly select them within the Screen session using a group of layout commands (see below).

2. Description of commands

Control keyboard shortcuts. To switch to the mode for entering control commands in screen, Ctrl-a is used by default. After pressing this combination, the actual control character or combination is entered, for example? (command help) or Ctrl-I (move to next area). To simplify the presentation, in the future only the control combinations themselves will be mentioned without the Ctrl-a switch.

Working with windows (pseudo-terminals, PTS) WithCreate a window with bash running in it.KClose a windowARename windowrEnable/Disable Line Wrap in Terminal" List windows Working with screen areas (regions) sSplit an area into two horizontallyI (pipe)Split an area into two verticallyTabSwitch to next screen areaQClose all other areas and leave the current one (which has focus) Working with Screen sessions dSend the session to the background and return to the system console

Screen internal commands. Available after entering the control character: (colon).

mousetrack onEnable mouse support for switching between areas. This blocks the ability to right-click copy/paste in the Putty ssh client window. For some reason this function disappears after “unpinning” the Screen session and returning to it.layout showconfiguration file" data-order="View the list of screen layouts specified in the configuration file">View the list of screen layouts specified in the configuration filelayout selectSelect the named screen layout specified in the configuration file either by number (n) or by name (title).layout save XXXsystem console." data-order="This command must be executed to save the current layout with the name XXX before detaching the Screen session, if no named layout is specified in the configuration file (for example, you just installed screen). Only in this case, the last layout is restored when you re-enter the Screen session from the system console.">This command must be executed to save the current layout with the name XXX before detaching the Screen session, if no named layout is specified in the configuration file (for example, you just installed screen).Only in this case, the last markup is restored when you re-enter the Screen session from the system console.layout autosave onThe markup autosave command is usually written in the configuration file. It remembers the named layout that was on the screen at the time the Screen session was “unpinned”. The command only affects markups that are explicitly specified in the configuration file. IN otherwise the current layout must be saved with the layout save XXX command (see above).quitClose all windows and end the session

/* Here you can add custom CSS for the current table */ /* Lean more about CSS: https://en.wikipedia.org/wiki/Cascading_Style_Sheets */ /* To prevent the use of styles to other tables use "# supsystic-table-1" as a base selector for example: #supsystic-table-1 ( ... ) #supsystic-table-1 tbody ( ... ) #supsystic-table-1 tbody tr ( ... ) * /

3. Example configuration file.screenrc

The global screen configuration file is located at /etc/screenrc, but for ease of use it is better to write a custom file, which should be located in the user's home directory ~/.screenrc. Note that there is a period at the beginning of the local file name.

I took the file from Github (.screenrc by joaopizani) as a basis and adjusted it to suit my needs:


So when we run Screen we will get a screen in the style Total Commander, which can be changed at any time by selecting a different layout. After returning to the Screen session from the system console, the layout will be the same as at the time of “unpin” thanks to the autosave function.

The effectiveness of administering GNU/Linux systems directly depends on the utilities used. The ability to complete a maximum of tasks in a minimum time is a priority goal of this process. Team screen Linux is one of the main system administrator tools.

By default screen is not standard utility in most distributions, but it works the same everywhere. Therefore, we will first consider its installation, and then the capabilities and basic commands.

The difference in installing the program on different distributions lies in the package management programs and their commands.

To install screen on Ubuntu, Linux Mint, Deepin and other Debian family systems, use the command apt:

sudo apt install screen

On Manjaro, Antergos and other Arch-like OSes, screen is located in the AUR, so use an appropriate package management program, e.g. aurman:

aurman -S screen

To install screen on systems based on Red Hat(for example Fedora and CentOS) use the utility dnf:

dnf install screen

Screen Command Basics

The general operating principle of GNU/Linux is to use small utilities that do one thing well. And this program is no exception.

The Linux screen command is a console program and has a clear definition as the name suggests - it is a window manager that shares one physical terminal among multiple processes. Suitable for direct or remote administration.

Program features

Screen allows you to switch between terminals that are running processes without interrupting them. This is especially effective when you need to build/install software and monitor disk space or use hardware resources.

Screen can divide the current terminal into a smaller number of windows with the ability to launch the same or other terminals in them. This makes the above task even easier, especially when using large monitors.

Also, several users can connect to one screen session. This is effective when training staff.

Basic screen commands

Open a terminal. To launch the first window, type:--

A new program session will be created and a welcome message will appear. To close it you need to press Space or Enter.

All control commands begin with a key combination Ctrl+a, followed by a letter or key combination. Letters of different registers perform different commands. Combinations can also be replaced with text. To enter it, click Ctrl+a And : , then enter text.

For example, to create a new window, press the combination in turn Ctrl+a and then c. To view a list of created windows, click Ctrl+a And w.

To go to any of the created session windows, use the combination Ctrl+a And number, which is assigned to him, or Ctrl+a And " , after which you need to select the required one with arrows. To move sequentially use Ctrl+a And n(next window) or p(previous window).

To split the screen horizontally, tap Ctrl+a And S. For example, to split a window horizontally write split.

A new window will be created below the current one and will not become active. To split vertically, click Ctrl+a And | (or enter split -v).

New empty windows were created. To switch between them use Ctrl+a And Tab(or write focus). The cursor position indicates which window is currently active. To hide a window use Ctrl+a And X(or type remove).

To open a terminal in a new window, click Ctr + a And c(or type screen).

To hide all windows and leave only the current one, use Ctrl+a And Q(or write only).

You can rename the window using Ctrl+a And A. A line will appear at the bottom with the option to replace the old name.

To hide all session windows, click Ctrl+a And \ . Confirm your decision by pressing y. To exit the window manager, click Ctrl+a And d.

The screen command has large set hot keys. A complete list of them can be viewed using Ctrl+a And ? .

When creating a new screen session, you can give it a name. This way you can create multiple sessions. To do this, use the option -S, followed by a name.

screen -S name-of-screen

You can view the list of running sessions using the command

The line identifying the session consists of several fields: id, name and status.

Meaning id appears in the list of all running processes. This means that it can be terminated using the command kill. You can close the current window using the combination Ctrl+a And k; confirm your decision by pressing y.

The status can have two states: Attached(involved) and Detached(unused). The second state is for sessions in which no processes have yet occurred.

To switch to the desired session, you should specify the -r parameter with its id or name.

These are all the basic screen Linux commands.

How to use screen Linux

Now let's look at how to use screen Linux in more detail.

Example 1. You need to track file changes in real time. To do this, in one window we will make changes to the new file, and in another window we will display the file with the command tail with option -f:

When erasing the contents, a message is displayed that the file is truncated, while this text, of course, is not written to it.

Example 2. You need to monitor the directory size in real time. In one window we record the data, in the other we display how the size of the home directory changes with the command watch with a two second delay.

Computer