You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派通过Systemd启动Streamlit脚本失败问题求助

树莓派Systemd启动Streamlit服务失败排查与修复

问题概述

已完成树莓派静态IP配置,编写的Streamlit启动脚本在本地运行正常,但通过Systemd配置开机自启后服务启动失败,systemctl status仅返回exit-code 1和重启过快的提示,无具体错误信息。

排查与修复步骤

1. 手动执行启动命令,定位具体错误

直接运行服务中指定的启动命令,能看到脚本执行时的详细报错:

/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py

大概率会发现缩进错误:你提供的Python脚本中,sys.argv和stcli.main()的缩进不符合Python语法规则,修正后的脚本如下(同时添加局域网访问参数):

from streamlit.web import cli as stcli
import sys

if __name__ == "__main__":
    script_path = "/home/pi/daqhats/examples/python/mcc128/daqhat_burster_plot.py"
    # 添加--server.address 0.0.0.0允许局域网设备访问
    sys.argv = ["streamlit", "run", script_path, "--server.address", "0.0.0.0"]
    stcli.main()

2. 查看Systemd详细日志

用journalctl获取服务启动时的完整输出,排查隐藏错误:

sudo journalctl -u run_streamlit_scripts.service -f

-f参数会实时显示日志,能捕获到依赖缺失、路径错误、权限不足等具体问题。

3. 修正权限与工作目录配置

  • 确保脚本拥有可执行权限:
chmod +x /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py
chmod +x /home/pi/daqhats/examples/python/mcc128/daqhat_burster_plot.py
  • 将Systemd服务的工作目录改为脚本所在路径(避免相对路径问题):
    修改服务文件中的WorkingDirectory字段:
WorkingDirectory=/home/pi/daqhats/examples/python/mcc128

4. 优化Systemd服务配置

调整服务文件,确保网络完全就绪后再启动,同时优化重启策略:

[Unit]
Description=Run streamlit scripts automatically
After=network-online.target
Wants=network-online.target  # 确保网络彻底连通后启动

[Service]
ExecStart=/usr/bin/python3 /home/pi/daqhats/examples/python/mcc128/run_streamlit_scripts.py
WorkingDirectory=/home/pi/daqhats/examples/python/mcc128
StandardOutput=journal+console
StandardError=journal+console
Restart=on-failure  # 仅在失败时重启,避免无限循环
User=pi
Environment="PATH=/usr/local/bin:/usr/bin:/bin"  # 确保系统能找到Streamlit

[Install]
WantedBy=multi-user.target

5. 重新加载服务并验证

修改完成后执行以下命令生效:

sudo systemctl daemon-reload
sudo systemctl restart run_streamlit_scripts.service
sudo systemctl status run_streamlit_scripts.service

内容的提问来源于stack exchange,提问作者keshmaster81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 23:15:59