Running streamlit scripts through systemd on a raspberry pi

I want to run my streamlit app on boot up with my raspberry pi to make my web server fully autonomous and accessible on my local network when I plug in the pi. To do this I’ve went through a few steps, the first being setting the IP of my pi to be static.

The second step I went through was creating a new script that can run my streamlit app(a different python file) from within the new python script. The code for that is shown below.

# run_streamlit.py
from streamlit.web import cli as stcli
import sys

if __name__ == "__main__":
  script_path = "/home/pi/daqhats/examples/python/mcc128/daqhat_burster_plot.py"
    sys.argv = ["streamlit", "run", script_path]
    stcli.main()

Up to this point, everything works. I am able to execute my streamlit apps directly through the run_streamlit_scripts.py file in VS code, rather than having to manually type streamlit run app.py in the terminal.

From here my intention was to make the run_streamlit_scripts file execute automatically when the pi is powered on so I can access the web server without ever having to type manually into the terminal.

To do this I went through the process of creating a service file in my systemd directory. I want to work with systemd as from what I’ve read its the most robust way to run files on startup compared to rc local and autostart.

[Unit]
Description=Run streamlit scripts automatically
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

This is where I am running into issues. What I believe should occur is that after a network connection is established, my run_streamlit_scripts python file should be executed meaning I can access my streamlit server at my static IP address. However this is not what occurs. When checking the status of my service using sudo systemctl status streamlit_app.service, I receive this error log:

run_streamlit_scripts.service - Run streamlit scripts automatically
     Loaded: loaded (/lib/systemd/system/run_streamlit_scripts.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-06-24 15:47:25 EDT; 3min 13s ago
   Duration: 68ms
    Process: 1037 ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py (code=exited, status=1/FAILURE)
   Main PID: 1037 (code=exited, status=1/FAILURE)
        CPU: 68ms

Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Scheduled restart job, restart counter is at 5.
Jun 24 15:47:25 raspberrypi systemd[1]: Stopped run_streamlit_scripts.service - Run streamlit scripts automatically.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Start request repeated too quickly.
Jun 24 15:47:25 raspberrypi systemd[1]: run_streamlit_scripts.service: Failed with result 'exit-code'.
Jun 24 15:47:25 raspberrypi systemd[1]: Failed to start run_streamlit_scripts.service - Run streamlit scripts automatically.

I’m not sure how to interpret this, any help would be appreciated, thanks!

Hi @rishi.anand

It seems to be an OS issue, perhaps you can explore more into the following relevant threads on systemctl: