Screen - text window manager

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

    So what is a Screen?


    Screen

    ScreenIs a window manager with VT100 / ANSI terminal emulation. In other words, this is the console in the console, i.e. By launching one terminal session, we can run several commands in parallel and observe their work. Of course, many can say why they need some kind of console window manager, when there is already gnome-terminal, konsole and other graphical terminal emulators that support tabs?

    Consider a situation where Screen is really necessary. Suppose you have the responsibility to remotely manage 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 is functioning normally, but one day something unexpected may happen and the network will fall, moreover, both your local computer and the remote server will function normally, but all the deleted sessions will be lost, the running programs will be killed, unsaved scripts will have to be written anew (it’s especially unpleasant when there is a rather long process that falls off at the very last stage and you have to start all over again).

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

    Another interesting Screen feature that I recently learned about is writing all the console output to a text file, usually bash records a list of commands (history), and screen can also write the output that was received as a result of these commands. In order to enable recording, press Ctrl-a H in the running screen, and to finish recording, just press this key combination again. Screen writes everything to the screenlog.n file, where n is an integer corresponding to the number of the Screen window. Usually I lose the resulting “demo” with such a set of commands (suppose the recording was from 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 main (everyday) Screen commands. In order to start Screen, you need to type the screen command in the terminal. After that, the terminal will not change externally (only text with a Screen license may appear. In order to execute special screen commands, you must first press the Ctrl-a keys (this is some prefix before all commands, it can be changed using the configuration file ., but in the article, I'll stick with the idea that it is the keyboard shortcut Ctrl-a is a prefix for screen-a) teams are some list of frequently used commands:

    Ctrl-a c - create a new window screen
    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, with all the commands continuing to execute
    Ctrl-a K - “kill” the selected window (you need it if the program in the window is frozen and does not want to die on its own)
    Ctrl-a "- display a list of all windows
    Ctrl-a A - change the name of the current window (it is convenient to distinguish between windows, for example, put the names" localhost "," ssh 1.2.3.4 ", etc.

    In order to get the full a list of commands you can use this guide .

    Consider another interesting feature of the Screen, namely the appointment of "bindings" on a variety of keyboard shortcuts. to begin with, I would say that to me seemed nudobnoy binding keys Ctrl-a as a certain prefix, much better to do this prefix combination Ctrl-\ keys. To do this, in the configuration .screenrc file, you must add the following line:

    the escape \ 034 \ 034

    still seemed uncomfortable every time typing Ctrl-a n and Ctrl-a pto move through the windows, it is better if this action is assigned to the function keys F1 and F2, respectively. To do this, add to the config file:

    bindkey -k k1 prev
    bindkey -k k2 next


    It would also be interesting to assign specific keys to launch a specific program in a new window. Consider, as an example, launching Vim in window No. 5 using the Ctrl-a e key combination. 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. This 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 will end (monitoring for inactivity) or when the program is being monitored in one window (for example, find) and the user wants to know when the program will output any lines to the terminal (activity monitoring). To enable this functionality, press the following key combination:

    Ctrl-a M (to track activity)
    Ctrl-a _ (to track inactivity)

    On this I want to end my article. I have described far from all the features of the Screen program, but the described functionality is sufficient for full 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 I ask you not to kick her much in comments, but healthy criticism is always welcome. Thanks for attention.

    Also popular now: