Outils pour utilisateurs

Outils du site


issue179:c_c

Ceci est une ancienne révision du document !


Most of us don’t think twice when typing at a terminal, it’s just there. What is actually happening is that you are typing commands into a shell that is interpreting your commands. By default, Ubuntu ships with bash, but you can install another if you like. I will focus on bash. The reason: if you ever get to use servers or containers on the internet, chances are good that it is bash.

Back in the day, we did not have Whatscrap or Telegram so we would use Screen to chat with each other; the session stays open when you connect from somewhere else. This was great for remote support, as you could be at a distant site and chat with the programmer or database admin whilst fixing the issue. (A sort of poor man’s direct chat). This is sometimes what you see in movies, when there is typing on the terminal without interaction. Wake up Neo… Screen ships with Ubuntu, so I would like you to do a ‘man screen’ and go to the end of the man pages. <insert RickRoll here> Long, isn’t it? Personally, I have not used it in years, as I used tmux, but that is not available everywhere. Thanks to one of my work colleagues, I was reminded of this multiplexer.

I like to think of Screen like you do a normal modern web browser, only with tabs you cannot see. You see, you can have multiple sessions in Screen.

You can move between these sessions (or tabs if you want to think of it that way: ‘CTRL+a’ and ‘w’ to see them from within a session) with keyboard shortcuts. You can even name sessions so that managing them is easier. I say, whatever works for you. The reason I think of it this way, is that you can have multiple “tabs” open in a screen session (pressing ‘CTRL+a’ and ‘c’ to open one).

So why would you want to use something like Screen? Even in our 99.9% uptime world, the connection from one place to another is not. Running scripts on a remote host that take a lot of time to parse data, for instance, may not complete, and you will have to restart them. This is a pain in the butt. This is where Screen comes in, allowing you to instantly reconnect to your session where the script may be running, even from a different PC!

Since the manpage is longer than the list of excuses politicians use, there is a quicker way to get help. Whilst you have an attached screen, press ‘CTRL+a’ and ‘?’. This will bring up what you need. Go ahead and try that now.

Caveat: Screen does not support all the “advanced” terminal bling that you may have become accustomed to, so fancy terminal prompts may disappear.

The power of Screen lies in these shortcuts. If you have a few open, pressing ‘CTRL+a’ and ‘a’ is really quick (pressing ‘a’ twice whilst ‘CTRL’ is held down, will tab you to the next session or tab). Let’s assume you need to do something else quickly; while your scripts are running, you can detach from the session with ‘CTRL+a’ and ‘d’. To reattach is as simple as typing ‘screen -r’ or ‘screen -x’ and you will be back where you left off.

Now for some practical learning; open a terminal emulator and fire up Screen. Open top or htop, now press ‘CTRL+a’ and ‘c’ to open another tab, and type ‘ls -la / -R’ so that it takes a while to complete. Type ‘CTRL+a’ and ‘n’ or ‘p’ to switch between the two. Open another, now type ‘CTRL+a’ and ‘w’ to see the current tabs or sessions. Switch to one and type exit. Now type ‘CTRL+a’ and ‘w’ again. What do you see?

These are the very basic navigation commands of Screen. The reason I typed the commands as ‘CTRL+a’ and ‘a’, not CTRL+a+a is that I want you to get used to understanding that the first part is an activation, before the next key. It is to not lead you astray, rather to get muscle memory going. A lot of the time, you will find that holding down CTRL and pressing the next two keys is the same as not holding down CTRL with the last keypress, but I have noticed some differences here in bash and other shells and terminal emulators and operating systems. Some commands are different, like the vertical split. Here you have to type ‘CTRL+a’ then release and press ‘SHIFT+|’ (pipe). Now to switch, we need to activate with ‘CTRL+a’ then press ‘Tab’. The same goes for the horizontal split, ‘CTRL+a’ and ‘SHIFT+s” – so learn when to remove your finger from the ‘CTRL’ key.

At any time, you can also have another session by typing Screen again, even if another terminal emulator is on your same machine, and if you go look at your list; they should be there. This comes in handy when you need to keep things apart, working with one group in one terminal and another group in a separate terminal. However, it does not stop you from connecting to the “other side” so-to-speak, so be aware.

I did tell you that you can name your Screen sessions; simply type: screen -S <name> and next time you do a Screen -ls command, you should see the name, instead of the number.

If you have opened more than 2 or three Screen sessions, you can also use CTRL+a and SHIFT+” (just one double-quote) to list them. If you do not get it right the first time, try again, I don’t know why, but my students used to struggle with this one.

For those a little more advanced than absolute beginners, there are some customizations in ~/.screenrc, but that is beyond the scope of this article, just wanted to spark interest.

issue179/c_c.1648385657.txt.gz · Dernière modification : 2022/03/27 14:54 de auntiee