用于自动启动 Streamlit 应用程序的 Systemd 服务文件

以下 systemd 服务文件用于运行 streamlit 应用程序。它假设 streamlit 应用程序位于 /home/user/myapp/app.py,并且 streamlit 应用程序应以用户 user 运行。

使用 pip install -U streamlit 安装 streamlit

将其保存在例如 /etc/systemd/system/myapp.service

myapp.service
[Unit]
Description=My streamlit app

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/opt/myapp
ExecStart=/usr/local/bin/streamlit run myapp.py --server.address 127.0.0.1 --server.port 18215
Restart=always
RestartSec=10s

[Install]
WantedBy=multi-user.target

根据需要调整 server.addressserver.port

然后,启用并启动服务:

enable-myapp.sh
sudo systemctl enable --now myapp

Check out similar posts by category: Python, Streamlit, Systemd