Console Into Mass. The Transit to the Light Side
Hi everyone! This is a text version on my speech given at WSD on the 26th of November in Kiev. The idea of writing this post was to share some key aspects with a wider audience, provide more details on the important points that have been discussed at the conference, give some illustrated examples and hear your arguments and opinion back.
The Escape Plan
The important note: a reader is expected to be a confident *nix OS user. Further in my examples, I will refer to many different utilities such as
sed, etc. yet without going deep into details. This was not my intention anyway.
I work in a big team and I often see how one and the same task assigned to several people can be fulfilled in several different ways. The sames goes for using a terminal. Meet IE6.
Imagine your favorite browser cannot work with tabs. Worse than that, it simply shuts down at the most inappropriate moment and doesn’t restore a session for you to continue to work.
The majority of users, however, when hear of the term “terminal” picture a black screen with white characters on it.
Now let’s imagine the same browser but with a terminal where you could enter an array of commands such as
node, etc. right there. Sounds like a remote future prospect, however, it isn’t so.
In these series of articles I’d like to prove usability and multifunctionality of a terminal so that those, having experience of using it, could get the most out of it.
Let’s begin. The topics I’m going to cover include console managers, bash-scripting and routine automation through bash-scripting.
For your convenience, I’ve created a repository of all examples and topics reviewed in this post on github, so, you’ve got access to the download files and have a chance to practise these commands on your own PC.
All examples and topics I’m going to cover in my articles are cross-platform compatible (Windows 10 stable, Mac OS X, Ubuntu). I’ve chosen Ubuntu OS, and so, whatever you see next is related to the Ubuntu environment. You can also use this ConEmu console emulator, though, if you’d decide to input some commands in it, you wouldn’t be able to do this, unfortunately.
Console managers. Part One. Screen.
My first meeting with screen was an absolute haphazard. One of my colleagues who assisted me in my work took notice of that I don’t use screen. “We need to fix it”, he said, and installed the screen on my computer, customized it for me and taught several basic key combinations.
To install the screen, it’s enough to enter the first command. After you do that, you will be able to open screen whenever you need by simply typing the command
screen in the terminal manager.
Basic Key Combinations
Why have I given a single link instead of listing all key combinations? Well, it is all pretty simple. Firstly, they all can be found in the manual (
man screen). Secondly, you don’t need to actually learn them by heart, all you need is to start using them.
By default screen doesn’t look very convenient. The application has got quite a lot of excessive features including license info and lacks a status bar. The good thing is, all these can be fixed by transferring the configuration file to a user’s home directory.
How screen Works
Let’s take a look at how screen works. You already know how to launch it, all you need is to type
screen in the terminal. Don’t forget to transfer the configuration file to a user’s home directory first.
- Working area. Layout.
- A status bar with tabs and other elements.
- A tab with the default name.
This is how screen looks like after you open it. Now let’s see how it works giving it a bit more complicated task to perform.
You will need to use a repository that I’ve mentioned above, so, better clone it to your PC.
Open the terminal and enter
screen. We’d need two-three tabs for a start. In order to create a new tab, press the following key combination:
The next thing to do is to rename these tabs:
To navigate through the tabs, use this keys combination:
Now, after we’ve done with the tabs, let’s separate layout into two independent regions pressing the keys:
To enable a new region you should activate it by keys:
Let’s open a new application in the left region. I’ve chosen midnight commander (feel free to make your choice). Enter the command:
In the right region we will open our test application. It’s a simple chat written in node.js and socket.io. I’ve taken this example from the official website socket.io and added to it some of the logging to show you that it works. Open the directory
%repoName%/screen/chat-example and enter the commands:
%repoName% — this is the directory’s name where the repository’s been cloned to.
If you’ve done everything right, you will see the next line on the screen:
It’s time to open your browser. Input the following
localhost:3000 command in a new tab, type some text in the input field and press “Enter” button. You see? It all works and works correctly, you’ve got the same text both in the browser and in the terminal, in the region where you entered the command
Let’s simulate disconnection, click “cross” to close the terminal, but keep the browser running and type in some more messages. As you can see, the process hasn’t been interrupted completely and we can see our messages appearing on the screen.
This means that screen can save sessions and all processes launched. Ok, let’s return to our session, open terminal and enter the command:
You’ve probably noticed right away that the original layout has changed. Not the most pleasant surprise to be fair. There are certain commands which you can use to save it. I guess I tried most of them and yet, couldn’t get it to work. It wouldn’t be perhaps such a big problem if you worked constantly only with two-three tabs, but what if you have six or nine tabs open, all sectioned both vertically and horizontally, than suddenly a connection’s gone and when it’s back, it all looks completely different? This isn’t handy of course. The only good thing is, all processes are saved.
For long I haven’t used terminal managers. Why you may ask? I simply didn’t know about them. I thought it was pretty standard to work with several putty open (Windows OS + remote connection to a server) and navigate them by pressing
Alt + Tabs. That looked real chaos but I learnt to deal with it.
Speaking of the drawbacks, I need to mention that screen cannot create independent layouts for each tab. Screen also doesn’t save layouts in case of a connection’s drop. Before 4.1 version screen couldn’t support vertical separation (that one was pretty critical for me). Whenever you create a new region, you also need to create a new window (
Ctrl + a, c), then shift to the needed directory, etc. To delete a region, you’d need to press one more key combination (
Ctrl + a, :remove). Honestly, for me screen was a real savior, but some of its features were quite hard to customize. Even now there are certain things I’d be eager to add, but simply cannot do so due to the screen implementation.
To sum it up, in this post we’ve reviewed screen, one of the popular terminal managers open for mass use. We’ve looked at how it can be installed, configured and customized. We’ve also seen the example of how it functions. Let me remind you that to check key combinations, the functions they’re assigned to perform in screen, etc. you only need to enter the following command:
Leaping ahead, in the next articles I’m going to tell you about a more advanced terminal manager — tmux. We’ll also compare screen and tmux.
This is all for now. Thanks for your attention. Special thanks to those, who’ve read the post to the end. Meet you all again pretty soon!