📜 ⬆️ ⬇️

Screen - text window manager

People who often work with Linux in the console had to deal with the problem of having several terminals, for example, in one of them the user edits the text file, and the other compiles the program. There are many ways to solve this problem, for example, to open several pseudo-terminals or to open several tabs. In this article I would like to consider a method that is not associated with any particular terminal: use the screen console window manager.

So, what is Screen?


Screen
')
Screen is a window manager with VT100 / ANSI terminal emulation. In other words, this is the console in the console, i.e. starting one terminal session we can run several commands in parallel and watch them work. Of course, many may say, why do we need a console window manager of some kind, when there is already gnome-terminal, konsole and other graphical terminal emulators that support tabs?

Consider the situation when Screen is really needed. Suppose you have the responsibility to remotely control a Linux server. Then, like any other admin, you connect to the server via SSH and execute various commands. If you need another window, then you create another SSH connection. And everything goes well as long as the network functions normally, but one day something unexpected may happen and the network drops, and your local computer and remote server will function normally, but all remote sessions will be lost, running programs will be killed, unsaved scripts will have to be written anew (especially nepryatno when there is a rather long process that collapses at the very last stage and you have to start all over again).

When using the Screen, we not only get rid of the problem of creating several SSH connections to the server, but in the event of a network failure, we can easily reconnect and all the running programs will continue to function. To solve the problem, simply re-create the SSH connection and type the command screen -dr in the console .

Another interesting feature of Screen, about which I recently learned, is the recording of the entire console output in a text file, usually bash records the command list (history), and the screen can record the output that was received as a result of these commands. In order to enable recording, you need to press Ctrl-a H in the running screen, and to complete the recording, just press the shortcut again. Screen will record everything in the screenlog.n file, where n is an integer corresponding to the number of the Screen window. Usually I play the resulting “demo” with such a set of commands (suppose that the recording took place at the window number 1):

time = 0.1
rows = `wc -l screenlog.1 | awk '{print $ 1}' `
for i in `seq 1 $ rows`; do head - $ i screenlog.1 | tail -1; sleep $ time; done;
reset

Here time is the delay time (the longer this time, the slower the output of the “demo” will be)

Consider the basic (everyday) Screen commands. In order to run Screen, you must type the screen command in the terminal. After that, the terminal does not change its appearance (only a screen may be displayed with a license.) In order to execute special screen commands, you must first press the Ctrl key (this is a prefix before all commands, you can change it using , but in the article I’ll stick to the idea that Ctrl-a is the prefix for Screen commands. Here’s some list of frequently used commands:

Ctrl-a c - create a new Screen window
Ctrl-a n - move to the next window
Ctrl-a p - move to the previous window
Ctrl-a d - disconnect from the current screen session, while all commands will continue
Ctrl-a K - “kill” the selected window (needed if the program in the window is frozen and does not want to die on its own)
Ctrl-a "- list all windows
Ctrl-a A - change the name of the current window (it is convenient to distinguish between the windows, for example, put the names “localhost”, “ssh 1.2.3.4”, etc.

In order to get a complete list of commands, you can use this guide .

Consider another interesting feature of the Screen, namely the appointment of "bindings" for various keyboard shortcuts. To begin with, I would say that I thought that the Ctrl-a key bindings were a bit like a prefix, it would be much better to do this with the Ctrl- \ key combination. To do this, the following line must be added to the .screenrc configuration file:

escape \ 034 \ 034

I also found it inconvenient to type Ctrl-a n and Ctrl-a p each time to move through the windows, it is better if this action is assigned to the function keys F1 and F2, respectively. To do this, add the file to the config:

bindkey -k k1 prev
bindkey -k k2 next

It would also be interesting to assign the launch of a specific program in a new window to certain keys. Consider, as an example, launching Vim in window 5 using the key combination Ctrl-a e. To do this, add the following to the config:

bind e screen -t 'Vim' 5 vim

Another feature of the Screen is the ability to monitor individual windows for activity or inactivity. Such functionality is convenient when there is a long compilation process in one window and the user is doing something in another and wants to know when the compilation ends (monitoring for inactivity) or when the program is monitored (eg find) in one window and the user wants to know when the program will give any lines to the terminal (monitoring the activity). To enable this functionality, you must press the following key combination:

Ctrl-a M (for tracking activity)
Ctrl-a _ (for disabling for inactivity)

At this point I want to finish my article. I have described far from all the features of the Screen program, but the described functionality is sufficient to complete the work. And in conclusion, I want to say that Screen is a very functional find for all Linux users working with the console.

Ps This is my first article on Habré, so please do not kick it at all in comments, but healthy criticism is always welcome. Thanks for attention.

Source: https://habr.com/ru/post/27315/


All Articles