To start a Python script at boot time on a Raspberry Pi 4 using a systemd service

Publié le
Bob Ross

The systemd method allows advanced control of the system. It can be customized to ensure that the script only starts when certain conditions are met. The Python script is executed here within an automated launch service at the boot of the RPI.

1 - The Python Script

The open-url.py script opens the URL of our robotics club in Firefox.
--> Update the package database  

sudo apt update

--> Update the system

sudo apt full-upgrade

--> Create a folder Scripts to save the Python script  

sudo mkdir /home/raspi/Scripts

--> Create the file open_url.py for editing with Geany

sudo geany

--> Enter the code in Geany

#!/usr/bin/python3
# Defines the path of the Python interpreter to use for executing the script

import subprocess
# Imports the subprocess module, which allows executing system commands from Python

url = "https://werobot.fr"
# Defines a variable containing the URL of the web page to open

firefox_command = ["firefox", url]
# Creates a list containing the command to execute to open Firefox with the specified URL

subprocess.run(firefox_command)
# Executes the command specified in firefox_command using the subprocess module
# This launches Firefox and opens the specified web page

--> Save it in the folder /home/raspi/Scripts with the name open-url.py


2 - The systemd Service

Location of service files in the directory tree:
--> Services provided in downloaded packages go into usr/lib/systemd
--> User-created services go into etc/systemd/system (such as the python script open-url.py)

--> Create the service file open-url.service with Geany for automating the open-ulr py script at startup

sudo geany

--> Enter the code in Geany

[Unit]
# Description of the service
Description=Opens a URL in the default browser

# This service will be started after the system is completely booted
After=multi-user.target

# This service requires network connection to be available
Wants=network-online.target

[Service]
# The service is executed when all system tasks are complete and it has become idle
Type=idle

# Command to execute to start the service
ExecStart=/home/frapoi/Scripts/open-url.py

# Restart the service in case of failure
Restart=on-failure

# Run the service as user 'frapoi'
User=frapoi

# Set the DISPLAY environment variable to indicate which screen to use
Environment=DISPLAY=:0

# Redirect standard output to a log file
StandardOutput=/var/log/open-url.log

# Redirect errors to a log file
StandardError=/var/log/open-url.error.log

[Install]
# Enable this service at system startup
WantedBy=multi-user.target

--> Save it in the folder /etc/systemd/system/ with the name open-url.service

--> Enable the file open-url.service

sudo systemctl enable open-url.service

--> Reboot the RPI

sudo reboot

The homepage of our site http://werobot.fr opens in Firefox.
The script can be adapted to automate the launch of any Python scripts and provides the basic principle.