SSH + screen + RPi

SSH + screen + RPi
Photo by Harrison Broadbent on Unsplash

Hello, long time no see!

Recently I had a situation in which I needed to have two processes running on a docker container, and this docker container was running on my RPi. So… I solved my situation using screen.

What is screen?

It is an awesome tool that allows us to have multiple terminals from a physical one. You can check more information here

How to install it?

Easy, just type this on your RPi:

sudo apt-get install screen

How to use it?

It is even easier! Here is a cheatsheet of the basic usage:

  1. To create a new session: screen -s session-name. After this you will be on that new terminal.
  2. To return to your original terminal: just hit ctrl + a  + d.
  3. To list all your terminals type: screen -ls
  4. To connect to one of the terminal you created (on step 1), type: screen -r session-name
  5. When you are on a session, you can kill that one using ctrl + k, this will ask you and you just have to hit “y” to kill that session

How do I use it?

Here is some context to my situation. I need to connect to my RPi, run a docker container, and run two processes on that docker container, here is what I do.

# connect to my rpi
ssh [email protected]
# run docker compose on the background
docker-compose up -d
# create a new session with screen, this will log me in into my new session
screen -S beat
# list docker containers running
docker ps
# connect the terminal on the docker image
docker exec -it name-of-the-container bash
# load some env vars
source env_vars
# start celery beat
celery beat --app config.celery -l info
# close beat session
ctrl + a + d
# create a new session with screen, this will log me in into my new session
screen -S worker
# list docker containers running
docker ps
# connect the terminal on the docker image
docker exec -it name-of-the-container bash
# load some env vars
source env_vars
# start celery worker
celery worker --app config.celery -l info
# close beat session
ctrl + a + d

And that is, after this, celery beat and worker will be running on background.