Run the App at Startup
This sets up two systemd services so an exhibit runs on its own when the Pi turns on:
- SERVICE-update pulls the latest main from GitHub at boot.
- SERVICE then runs main.py fullscreen on the desktop.
The exhibit runs from its own checkout in /opt/dpea/REPO_NAME, which gets reset to main on every boot. You develop in a separate checkout in ~/src/REPO_NAME, so your edits never fight with the running exhibit.
Throughout this page, REPO_NAME is the repo’s exact name on GitHub and SERVICE is that name in lowercase. For example, Newtons-Cradle runs as newtons-cradle.
Before you start
- The Pi is flashed with the DPEA Raspberry Pi image and is connected to the internet.
- The Pi has a deploy key for this repo.
- The repo is in the dpengineering org, uses uv (it has pyproject.toml and uv.lock), and the app starts from main.py at the repo root.
1. Add the deploy files to the repo
On your computer, make a deploy folder at the root of the repo with these four files:
deploy/
setup.sh
update.sh
systemd/
app.service
app-update.service
| File | What it does |
|---|---|
| deploy/setup.sh | One-time setup. Clones the exhibit checkout, installs dependencies, and installs and enables both services. |
| deploy/update.sh | Runs at every boot. Resets the exhibit checkout to the latest main. |
| deploy/systemd/app-update.service | Runs update.sh at boot once the network is up. Installed as SERVICE-update.service. |
| deploy/systemd/app.service | Runs main.py after the update finishes. Installed as SERVICE.service. |
Create each file and paste in the contents below. Hover over a block and click the copy button in its corner.
deploy/setup.sh
After pasting, change REPO_NAME on line 5 to the repo’s exact name on GitHub, for example REPO_NAME=Newtons-Cradle. Capitalization matters. This is the only line you change in any of the files.
#!/bin/bash
set -euo pipefail
# Set this to the repo's exact name on GitHub.
REPO_NAME=Your-Repo-Name
SERVICE=${REPO_NAME,,}
REPO=git@github.com:dpengineering/$REPO_NAME.git
EXHIBIT_DIR=/opt/dpea/$REPO_NAME
fail() {
echo "$1" >&2
exit 1
}
[[ $EUID -ne 0 ]] || fail "Run setup.sh as the exhibit user, not root."
command -v uv >/dev/null || fail "uv is missing. Flash the DPEA Raspberry Pi image."
# Clone the exhibit checkout over the deploy key, or reuse one that already pulls from this repo.
if [[ -d $EXHIBIT_DIR/.git ]]; then
origin=$(git -C "$EXHIBIT_DIR" remote get-url origin)
[[ $origin == "$REPO" ]] || fail "$EXHIBIT_DIR pulls from $origin, expected $REPO."
else
sudo install -d -o "$USER" -g "$(id -gn)" "$(dirname "$EXHIBIT_DIR")"
git clone "$REPO" "$EXHIBIT_DIR"
fi
# Install the exact pinned dependencies into the checkout's .venv.
(cd "$EXHIBIT_DIR" && uv sync --locked --no-dev)
# Fill in this Pi's user, checkout path, and names, install both systemd units, and start them on every boot.
install_unit() {
sed -e "s|@USER@|$USER|g" -e "s|@EXHIBIT_DIR@|$EXHIBIT_DIR|g" \
-e "s|@SERVICE@|$SERVICE|g" -e "s|@REPO_NAME@|$REPO_NAME|g" \
"$EXHIBIT_DIR/deploy/systemd/$1" | sudo tee "/etc/systemd/system/$2" >/dev/null
}
install_unit app-update.service "$SERVICE-update.service"
install_unit app.service "$SERVICE.service"
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE-update.service" "$SERVICE.service"
# Let the admin Quit button stop the service without a password. visudo checks the rule before it goes live.
sudoers=$(mktemp)
echo "$USER ALL=(root) NOPASSWD: /usr/bin/systemctl stop $SERVICE.service" >"$sudoers"
sudo visudo -cqf "$sudoers"
sudo install -m 440 "$sudoers" "/etc/sudoers.d/$SERVICE"
rm "$sudoers"
# Boot straight into the desktop the app draws on, and never blank the screen.
sudo raspi-config nonint do_boot_behaviour B4
sudo raspi-config nonint do_blanking 1
echo "Setup complete. Reboot to start $REPO_NAME."
deploy/update.sh
#!/bin/bash
set -euo pipefail
main() {
# Work from the repo root, and make git fail fast instead of prompting when GitHub is unreachable.
cd "$(dirname "$(readlink -f "$0")")/.."
export GIT_SSH_COMMAND="ssh -o BatchMode=yes -o ConnectTimeout=10"
# Remember the running commit, and keep it if main can't be fetched (e.g. no internet).
local previous
previous=$(git rev-parse HEAD)
if ! timeout 30 git fetch --quiet origin main; then
echo "Fetch failed, keeping $(git rev-parse --short HEAD)"
return
fi
# Make the checkout exactly match main, discarding any local edits.
git reset --quiet --hard origin/main
# If main's dependencies won't install, go back to the previous commit and its already-cached packages.
if ! uv sync --locked --no-dev; then
echo "uv sync failed at $(git rev-parse --short HEAD), restoring ${previous:0:7}" >&2
git reset --quiet --hard "$previous"
uv sync --locked --no-dev --offline
fi
echo "Running $(git rev-parse --short HEAD)"
}
main "$@"
exit
deploy/systemd/app.service
# Runs at boot once the update service finishes. Restarts only after a clean exit (admin Restart), so a crash leaves it off.
# Starts main.py on the desktop's display (DISPLAY=:0) from the exhibit checkout's venv.
[Unit]
Description=@REPO_NAME@ exhibit
Wants=@SERVICE@-update.service
After=@SERVICE@-update.service
[Service]
User=@USER@
WorkingDirectory=@EXHIBIT_DIR@
Environment=DISPLAY=:0
# Kivy logs to the console, which systemd sends to the journal, so skip its ~/.kivy/logs files.
Environment=KIVY_NO_FILELOG=1
# Fill whatever screen is attached at its native resolution.
Environment=KCFG_GRAPHICS_FULLSCREEN=auto
ExecStart=@EXHIBIT_DIR@/.venv/bin/python -u main.py
Restart=on-success
RestartSec=3
[Install]
WantedBy=graphical.target
deploy/systemd/app-update.service
# Runs once at boot, after the network is up and before the exhibit starts.
# Pulls the latest main into the exhibit checkout, keeping the current commit if that fails.
[Unit]
Description=Update @REPO_NAME@ from main
Wants=network-online.target
After=network-online.target
Before=@SERVICE@.service
[Service]
Type=oneshot
User=@USER@
ExecStart=@EXHIBIT_DIR@/deploy/update.sh
[Install]
WantedBy=multi-user.target
Make both scripts executable, then commit the deploy folder and push it to main:
chmod +x deploy/setup.sh deploy/update.sh
2. Clone a development checkout on the Pi
git clone git@github.com:dpengineering/REPO_NAME.git ~/src/REPO_NAME
This is where you edit and test on the Pi. Don’t edit files in /opt/dpea/REPO_NAME, since the next boot erases them.
3. Run setup
~/src/REPO_NAME/deploy/setup.sh
Run it as your normal user, not with sudo. It asks for your password when it needs it. Setup:
- clones the exhibit checkout into /opt/dpea/REPO_NAME
- installs the exact dependency versions from uv.lock
- installs and enables SERVICE-update and SERVICE
- lets the app stop its own service without a password, for an admin Quit button
- logs in to the desktop at boot and turns off screen blanking
Then reboot:
sudo reboot
4. Check that it’s running
The app should open fullscreen after the Pi boots. To check the services:
systemctl status SERVICE
Read this boot’s logs, including why the app stopped:
journalctl -u SERVICE-update -b
journalctl -u SERVICE -b
How updates work
Push to main, then reboot the Pi. On every boot, SERVICE-update fetches main and resets the exhibit checkout to it, then SERVICE starts the app.
- Without internet, the exhibit keeps running the code it already has.
- If the new commit’s dependencies fail to install, it goes back to the previous commit.
- Files that git ignores, like a per-machine settings file, are never touched by updates.
When the app exits
What the service does next depends on how the app exited:
| How the app exits | What happens |
|---|---|
| Normally (exit code 0), like pressing Escape on a keyboard | The service starts the app again after 3 seconds. |
| With an error, like an uncaught exception | The app stays off until the Pi is power-cycled, so a broken exhibit doesn’t restart over and over. |
| The service is stopped | The app stays off until the Pi is power-cycled. |
Admin Restart and Quit buttons
If the app has an admin screen, its Restart and Quit buttons use this behavior:
- Restart resets a misbehaving session. Make the hardware safe (stop motion, disable motors), then exit the app normally, for example with App.get_running_app().stop() in Kivy. The service brings it back.
- Quit turns the exhibit off until it’s power-cycled. Exiting isn’t enough, since the service would just restart it. Make the hardware safe, then stop the service. setup.sh lets the app run this one command without a password:
import subprocess
subprocess.run(["sudo", "systemctl", "stop", "SERVICE.service"], check=True)
Replace SERVICE with the service name, like newtons-cradle.service.
Working on the Pi
The running exhibit holds the hardware. Stop it before running your development checkout:
sudo systemctl stop SERVICE
Start it again when you’re done:
sudo systemctl start SERVICE
Setup installs the services from the exhibit checkout, not your development checkout. After changing anything in deploy/systemd, push it to main, reboot so the exhibit checkout pulls it, then run setup.sh again and reboot.