HowTo: RaspberryPI - Multitasking and "I'll continue with that later" with Screen

Working in the console has a few disadvantages that are not immediately obvious. For example, in Windows you are used to being able to switch between windows.
At first glance, simply “hopping” into the browser, copying a link and back into a Word document that is open at the same time to paste the link there is not possible in the console.

Connection problems can sometimes have annoying consequences:
For example, if your RasPi is somewhere in the middle of nowhere and does its job as a lonely weather station with a very sporadic network connection, it can happen that your console connection is interrupted. This also means that your console connection will be interrupted and all processes you have started on the RasPi will also be terminated.

If you have just run a process that does something important for 15 minutes and now the connection has been lost for the third time after 14 minutes, one or the other will slowly start chewing on the tabletop in anger.

Um die Zähne zu schonen und auch manche Arbeitsabläufe zu vereinfachen wurde deswegen das Programm „Screen“ erfunden.

This allows you to launch multiple virtual consoles in one console session, switch between them and continue even after your connection has been interrupted and restored.


Safety instructions

I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/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 make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂 


Overview

Before you can start installing Screen, you should have prepared the Raspberry Pi so that it can be accessed via the network and via SSH.
The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPI - The First Configuration!
RaspberryPi – Control the RaspberryPi via SSH

Required tool:
-no-

Required material:

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


Log in to the RaspberryPi via SSH

To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.

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

Update package management

The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.

To start the update of the package management you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long ago your last update of the package management was, this process can now take a while. The lists that refer to the individual package sources are updated first.
After that, the packages themselves are updated. Since additional memory is occupied, you will be asked again for your consent. You have to confirm this with a "J" and "Enter".
Once the update is complete, you will see a small summary of the duration and scope of the update.

Install screen

You start the installation of Screen with the following command:

sudo apt-get install screen

During the installation of the updates and the subsequent packages, you may be asked whether you agree that the modules to be installed will take up additional storage space. You confirm these questions with a “j” 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 closed. To start a virtual console from the real console you have to enter the following command:

screen bash

You will then find yourself in a new/fresh virtual console environment that is visually no different from a real console environment. You can work in this just like in the real console environment.

Wenn ihr nun aber einen Prozess habt der sehr lange läuft so könnt ihr diesen, einmal in einer virtuellen Konsole gestartet, mit dem Tastendruck STRG+A+D in den Hintergrund verschieben. (Ihr drückt also erst STRG, haltet diese Taste gedrückt und drückt dann „A“ und dann danach „D“)

The virtual console then disappears and you are back in the real console. You can now close these or do something else. Your Virtual Console input and processes will continue to be saved and run in the background.

You could now also start another virtual console and let it run 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 see which virtual consoles are currently active. This can be done with the following command:

screen -list

After you have executed this command, all active virtual consoles will be displayed.

In diesem Fall sind gerade zwei virtuelle Konsolen aktiv. Die obere hat den Namen „1435.pts-0.magicMirror“, wurde am 15.11.2017 um 15:59:40 Uhr gestartet und ist aktuell „detached“(=abgetrennt) also nicht mit der realen Konsole verbunden.

Die untere virtuelle Konsole hat den Namen „1413.pts-0.magicMirror“, wurde am 15.11.2017 um 15:50:35 Uhr gestartet und ist aktuell „detached“(=abgetrennt) also nicht direkt mit der realen Konsole verbunden.


Connect to an existing Screen instance

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

1st option

„screen –list“ zeigt euch nur eine im Hintergrund aktive virtuelle Konsole an:
In this case the following command is sufficient:

screen -r

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


2nd option

„screen –list“ zeigt euch mehrere, im Hintergrund aktive, virtuelle Konsolen an:

In diesem Fall müsst hier zusätzlich zum Befehl „screen –r“ auch noch den Namen der virtuellen Konsole angeben mit der ihr euch verbinden wollt.

Werden euch mit „screen –list“ zum Beispiel(wie auf dem BIld zu sehen) zwei virtuelle Konsolen mit den Namen „1435.pts-0.magicMirror“ und „1413.pts-0.magicMirror“ angezeigt und ihr wollt die virtuelle Konsole mit dem Namen „1435.pts-0.magicMirror“ öffnen so müsst ihr folgenden Befehl eingeben:

screen –r 1435.pts-0.magicMirror

Pro tip:
In diesem Fall würde auch schon „screen -r 1435“ ausreichen, weil so auch schon eindeutig angegeben ist welche virtuelle Konsole verbunden werden soll.


End a screen instance

There are two ways to end an active virtual console.

1st option:

Ihr verbindet euch mit der virtuellen Konsole die ihr schließen wollt und drückt STRG+D. Dies schließt die aktuell verbundene virtuelle Konsole ohne weitere Warnungen. Alternativ könnt ihr auch „exit“ eingeben. Auch dies beendet die aktive virtuelle Konsole.


2nd option

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

Nehmen wir an ihr wollt die virtuelle Konsole mit dem Namen „wichtigeVirtuelleKonsole“ beenden. Dann müsst ihr folgenden Befehl eingeben:

screen -X -S importantVirtualConsole quit

This will stop the virtual console running in the background without you having to connect to it first.

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


Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at ko-fi.com       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.