Ceci est une ancienne révision du document !
Last month, we looked a bit more in-depth at jobs, foreground and background processes. This allows me to segue into screen. Though we chatted about it before, (2022?), it was to segue into tmux. We can touch on tmux again - it is great - or tilix, but know that in the real world, you will probably connect to a jump host or a server that has only the OS installed, so things like tmux will not be available for you to use. Hence why I like to start off with screen, not that I’m old-skool or anything, I like tmux/tilix more, but trust me, you will get stuck with screen more often than not.
What is screen? Well, screen is a virtual terminal that allows you to run jobs, even after closing your terminal (continues the process). To demonstrate what I mean, open a terminal and type: evince
Your document viewer opens. Now close the terminal and you should get a warning, but close it anyway. What happened to evince? So are we on the same page? The evince process was terminated when your terminal emulator was closed. We don’t want this when working on a remote machine. Imagine you have to zip up log files on remote machines and copy them elsewhere and you also need to go home as it is after five and you would rather not sit around for another hour at work, or you are busy hacking your school computer, running a brute-force attack, but it could take hours and you need to be home by 9, you don’t want to stop dead in the water and start again from scratch tomorrow, you want it to finish.
What is nice about screen, is that you can have a few instances running at once and switch between the processes running in each. However, do not assume it is installed. You may need to add it with: sudo apt install screen – on both ends where you intend to use it. The man page for screen is very long, and it uses that emacs type notation for key bindings, that look like this: C-a meaning CTRL+a. (Important, file for later use).
Fire up your terminal and type:
screen
If you are directly on a TTY, you should not see this, it would seem that nothing happened.
However, we can sort of get a grasp by doing the following.
Type:
echo “start”
press CTRL+a let go of the keys and press c
echo “middle”
press CTRL+a let go of the keys and press c
echo “end”
press CTRL+a let go of the keys and press p
press CTRL+a let go of the keys and press n
What happened?
Now:
press CTRL+a let go of the keys and press 0
press CTRL+a let go of the keys and press 2
press CTRL+a let go of the keys and press “
You should be able to navigate with the up and down arrow key.
If you use it often, it will be good to know:
press CTRL+a let go of the keys and press A (Capital a) and name this screen something.
I renamed the middle one, “Centre” and when I bring up my list, I can see it. Your turn.
So now, instead of it just saying “bash”, it has a name and you should see a ‘*’ indicating which screen you are currently on. Start a ping in one of those screens and switch away and go back.
If you were wide awake, you may have noticed “detach” in the man page, obviously it is:
press CTRL+a let go of the keys and press d.
You should have your normal bash prompt back (some distributions will let you know with a message, like Ubuntu, but don’t count on it).
Now for the part I want you to compare with what happened to evince, close the terminal. Open a new one, it does not even have to be a gnome terminal.
Now type:
screen -ls
That (detached) message, is the one we just disconnected from, and since it is “recent”, we can use: screen -R -to reattach to that session, or if you detached more than one or had multiple sessions open, like I did, you can use the PID displayed, to connect to it, but that is a lowercase “r”. (Check the man page for more “d” and “r” options…).
Try it yourself quickly, I’ll wait….
One last thing: just like screens, you can also name your sessions (though I have never used it, as usually my session is one screen to one server), and you can bake that into a configuration file, but I feel we are stepping outside newbie territory. When starting a screen session, use -S (capital “S”) followed by a name (I named it FCM). You can also name a session from *inside screen, you just need to list all the open screens first, with:
press CTRL+a let go of the keys and press “
press CTRL+a let go of the keys and press :
You should now see a “:” at the bottom of the terminal. Type sessionname (one word)
Start naming and press <enter>
Now for the fun part, to kill a screen:
press CTRL+a let go of the keys and press k
and what about killall, I hear you ask, well:
press CTRL+a let go of the keys and press \
And that should cover the basics for any newbie, I really don’t think you will need more in a loooong time, but have a look at the man page, and see that this is a grand daddy page! There are options to fit everyone’s use case. I hope you followed along in your terminal, or it will be forgotten tomorrow.
Any mistakes, please write to: misc@fullcirclemagazine.org