How to Run Long-Running Scripts on a Raspberry Pi

When I started out with a Raspberry Pi, I often used it to process long-running scripts, like continuously monitoring a video stream or a small script that gathered data from the web. The problem I ran into was how to stay connected to the Pi long enough for the script to not terminate.

You can hook up a keyboard, monitor and run the pi that way – but I prefer working with my Raspberry Pi over SSH, so I can have all the conveniences of my regular computer.

So how do you keep a script on your Pi running for a long time via SSH so it doesn’t time out and fail? There are several ways.

What you will need for this project

How to Run Long-Running Scripts on Your Raspberry Pi

Before you begin, set up your Raspberry Pi. If you haven’t already, check out our article on how to configure a raspberry pi for the first time or how to make a Raspberry Pi headless setup (without keyboard and screen). For this project, we recommend a Raspberry Pi headless setup.

Depending on the project you’re working on, we’ll cover two methods for handling long-running scripts. If you only run a script once or twice, use the tmux method. If you’re setting up your Pi for a long-term project and want a script that automatically restarts, switch to the “service” method.

Using tmux on your Raspberry Pi

The tmux package lets you create a separate shell that you can detach from and let it run in the background. If you had a single program that you know is going to take a long time (a make build command for example), you can create a tmux session, run your long command, detach from that session, and log out of the pi with no worries . As long as you don’t interrupt power to the pi, your script should continue to run. You can attach to the session and check it periodically and detach it to let it run.

If this sounds like your use case; here’s how to use it.

1. Install the tmux package.

sudo apt-get install -y tmux

2. Create a new tmux session.

# pick any session name you like
tmux new -s your_session_name

python3 my_really_long_running_script.py

3. Log out of the session using the following keys:

Hit “Ctrl + B”
Then hit “D”

You will be returned to your original session, with your long running script running in the background. You can log out at this point.

4. To attach to session to check how it goes just run the following

# list all the sessions running
tmux list-sessions

# attach to your session
tmux a -t your_session_name

And that’s all. You can detach again as described in step 4 if you wish. Tmux is best used for commands that you run once, like an installation step. If you’re looking to run something long-term, continuously, with an automatic restart, you’ll want to create a service.

Create a service on your Raspberry Pi

A service is best used for an application that you want to run continuously on your pi. Maybe it’s a script that constantly checks data from the weather station and sends it to the cloud, or a simple app that listens for color change requests from an LED strip. If you’re looking for something that can handle reboots automatically and start when the Pi boots, a service is a great option.

1. To create a service, start with test if the script you want to run works correctly. Be sure to use the full file path when invoking it.

python3 /home/pi/my_script.py

2. Next we will create a file in /etc/systemd/system to define our service:

sudo touch /etc/systemd/system/myscript.service

3. Next we will modify the file by adding the required information to set the service and allow it to restart if it fails.

sudo nano /etc/systemd/system/myscript.service

Add the following lines to the file

[Unit]
Description=My long running script
After=network.target
StartLimitIntervalSec=0[Service]
Type=simple
User=pi
Restart=always
RestartSec=1
ExecStart=python3 /home/pi/my_script.py

[Install]
WantedBy=multi-user.target

4. Once you have saved your file, start and activate the service – allowing it to run immediately, and also when the pi restarts.

sudo systemctl start myscript && sudo systemctl enable myscript

5. Your script will now run on your pi in the background, automatically restarting on failure. You can check it with the following command:

sudo systemctl status myscript

And that’s all. Many services are already running on your Pi as part of its normal operation. By setting and enabling this service, you are simply adding your script to the rest of the things your Pi does just by being plugged in. If at any time you need to stop the service, you can do so with the following command:

sudo systemctl stop myscript

There are many other ways to run scripts in the background, but for Raspberry Pi projects, these two are my favorite commands. Good build!

# pick any session name you like
tmux new -s your_session_name

About Ethel Partin

Check Also

Heartwarming video of Cadiz goalkeeper running to pits with defibrillator to treat spotted fan ▷ SportsBrief.com

The Barcelona and Cadiz players left the pitch with their play on hiatus due to …