Search code examples
pythonservicesystemdmavlink

AssertionError when launching python via systemd


I seem to be getting an AssertionError with my Python script, but only when I launch it through systemd. Otherwise, it works fine if launched manually from the command line. I thought perhaps it's a permissions thing, but it also runs fine being manually executed as root.

enter image description here

This is the error. The python file this error is in can be found here: https://github.com/lericson/mavlink-tools/tree/master, I'm using the logdl.py.

I'm almost certain this is not an issue with the actual Python itself as it runs perfectly fine being ran manually from command line. Any ideas? A quick Google revealed a bug with old pipenv but I made sure mine is all up to date.

The systemd service:

[Unit]
Description=Logger Service
After=network-online.target

[Service]
ExecStart=/home/logger/start_logger.sh

[Install]
WantedBy=multi-user.target

start_logger.sh:

#!/bin/sh

/home/logger/.venv/bin/python3 /home/logger/logger/main.py

From here, main.py waits until it detects the flight controller device. It then launches logdl.py as a subprocess and waits for it to finish. It also blinks an LED so the user knows when the download has finished, then waits until the flight controller is removed before looping back to detecting it again.

UPDATE:

I removed the shell script from the process, realised it was probably unnecessary. Now, the service file calls /home/logger/.venv/python3 /home/logger/logger/main.py itself. However, now it logs with a serial exception every single time a Flight Controller is connected.

Error now:

serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Once again, error is not present running the service command manually in the terminal. It's only present running it as a service


Solution

    1. AssertionError was what I was seeing in journalctl but turns out the error was actually the SerialException, causing the assertion to fail

    2. The SerialException was caused by 2 things, the first being that serial console was enabled on the Pi Zero (disable this through raspi-config). The second was adding the user to dialout.

    I am still confused why it worked on command line with no issue but not as a service, but doing the above has made it work as a service so problem solved. If someone could explain why this is the case that would be fantastic.