HowTo: RaspberryPI – Multitasking and “I’ll continue that later” with Screen

The work in the console has a few disadvantages that are not directly apparent. For example, Windows is used to switching between windows.
Just “jump” into the browser to copy a link and back to a simultaneously open Word document to insert the link there is not possible in the console at first glance.

Even connection problems can sometimes have annoying consequences:
For example, if your RasPi is somewhere in the far away surounding and does its job as a lonely weather station with a very sporadic network connection, it may happen that your connection to the device is interrupted. This also means that your console connection is interrupted and thus all processes started by you on the RasPi will be terminated.

If you have just completed a process that makes 15 minutes something important and now the connection is broken for the third time after 14 minutes, so one or the other starts to chew with anger at his table.

That’s why the “Screen” program was invented to save teeth and simplify some workflows.

This allows you to start, switch between, and continue in a console session with multiple virtual consoles after your connection has been broken and restored.

Hints for our lovely english readers: Basically, many of the articles on Nerdiy.de are translations from the original german articles. Therefore, it may happen here and there that some illustrations are not available in english and that some translations are weird/strange/full of mistakes or generally totaly wrong. So if you find some obvious (or also not obvious) mistakes don't hesitate to leave us a hint about that in the comment section. 
Also please don't get confused, that instead of a "dot" often a "comma" is used as decimal separator. 🙂


Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/

Affiliate links / advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Requirements

Helpful Articles:
Before you start with this article, you should have prepared the RaspberryPi so far that it can be reached via the network and controlled by SSH.
RaspberryPi – Setting up for Nerdiys!
RaspberryPi – The first configuration
RaspberryPi – Controlling the RaspberryPi via SSH

Required tools:
-none-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in via SSH on the RaspberryPi

To get started, you first need to log in to RasPi with SSH on Putty. How to do it is described in the article

After entering your username and password you can start to enter the first commands.

Update package management

The package management in Linux is a "central point" over which various software packages can be installed. For this to work reliably, the lists and sources of package management should be updated before installing any new packages.

To start the package management update you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long your last update of the package management is, this process can take some time. First, the lists are updated in which the individual repositories are referenced.
Then the packages themselves are updated. As this additional memory is occupied, you will again asked for your consent. You have to confirm this with a "J"(in German) or a "Y" and "Enter".
When the update is complete, you will see a small summary of the duration and scope of the update.

Install screen

The installation of Screen starts with the following command:

sudo apt-get install screen

During the installation of the updates and subsequent packages, there may be questions as to whether you agree that additional space will be occupied by the modules you are installing. You confirm these demands with a “j”(in German. Or an “y”) and enter. The installation will then continue.


Start a virtual console with screen

Screen is now installed and you want to start your first virtual console. This virtual console will continue to run even if your real console is quit. To start a virtual console from the real console you need to enter the following command:

screen bash

Then you will find yourself in a new/fresh virtual console environment that does not visually differ from a real console environment. In this you can work just like in the real console environment.

But if you have a process that runs for a very long time, you can move it to the background, once in a virtual console, by pressing CTRL + A + D. (So you first press CTRL, keep this key pressed and then press “A” and then “D”)

The virtual console disappears and you are back in the real console. You can now close these or do something else as well. Your virtual console inputs and processes will continue to be saved and executed in the background.

You could now start another virtual console and run it in parallel to the previously started virtual console.


Show all screen instances

In order to switch from the real console back to the virtual console, you should first check which virtual consoles are currently active. This is done with the following command:

screen –list

After you execute this command you will see all active virtual consoles.

In this case, there are currently two virtual consoles active. The upper one has the name “1435.pts-0.magicMirror”, was started on 15.11.2017 at 15:59:40 clock and is currently “detached” (= disconnected) so not connected to the real console.

The lower virtual console has the name “1413.pts-0.magicMirror”, was started on 15.11.2017 at 15:50:35 clock and is currently “detached” (= disconnected) so not directly connected to the real console.


Connect to an existing Screen instance

In order to display a virtual console so that you can work in it, there are basically two options.

1st possibility

“screen -list” shows you only a virtual console active in the background:
In this case, the following command is sufficient:

screen –r

This will cause your real console to connect to the only available virtual console.


2nd possibility

“screen -list” shows you several, in the background active, virtual consoles:

In this case, in addition to the “screen -r” command, you also need to specify the name of the virtual console you want to connect to.

For example, with “screen-list” (as shown on the picture), you’ll see two virtual consoles named “1435.pts-0.magicMirror” and “1413.pts-0.magicMirror” and you want the virtual console with the name “1435.pts-0.magicMirror” open, you need to enter the following command:

screen –r 1435.pts-0.magicMirror

Professional tip:
In this case, even “screen -r 1435” would be sufficient, because it is already clearly indicated which virtual console should be connected.


End a screen instance

There are two ways to end an active virtual console.

1st possibility

You connect to the virtual console you want to close and press CTRL + D. This closes the currently connected virtual console without any further warnings. Alternatively, you can also enter “exit”. This also stops the active virtual console.


2nd possibility

You are in the real console. Similar to reconnecting to an existing screen instance, you can also quit one.

Let’s say you want to quit the virtual console named “important virtual console”. Then you have to enter the following command:

screen -X -S importantvirtualconsole quit

This stops the virtual console running in the background without you having to connect to it beforehand.

So now you know how to use “screen” to create and manage various virtual consoles on the RasPi.


I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

Fab

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *