Running Tasks as a System Daemon with systemd
If you want to run a Streamlit dashboard, you can use screen, but a more reliable approach is to run it as a system daemon with systemd. This post is based on a helpful tutorial by Shubham Dipt and adds a few practical details for real-world setups.
1. Create a Service File
SSH into your EC2 instance and move to the systemd unit directory:
cd /etc/systemd/system
Create a service file, for example epcaldash.service, and add the following:
[Unit]
Description=EPCAL Dashboard
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/epcal/epcal_dashboard
ExecStart=/home/ubuntu/miniconda3/envs/py39/bin/streamlit run epcal_dashboard.py
Restart=always
[Install]
WantedBy=multi-user.target
Notes:
-
WorkingDirectoryshould point to your Streamlit app directory. -
ExecStartmust use the correct Streamlit executable (e.g., from your virtual environment).
2. Optional: Run a Shell Script First
If you need extra commands (e.g., iptables for port forwarding), you can point ExecStart to a shell script instead:
[Unit]
Description=EPCAL Dashboard
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/epcal/epcal_dashboard
ExecStart=/bin/bash epcaldash_service.sh
Restart=always
[Install]
WantedBy=multi-user.target
Example epcaldash_service.sh:
#! /bin/bash
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 8501
/home/ubuntu/miniconda3/envs/py39/bin/streamlit run /home/ubuntu/epcal/epcal_dashboard/epcal_dashboard.py
Make it executable:
chmod +x epcaldash_service.sh
3. Reload, Start, and Enable
sudo systemctl daemon-reload
sudo systemctl start epcaldash.service
sudo systemctl status epcaldash.service
Enable on boot:
sudo systemctl enable epcaldash.service
Disable on boot:
sudo systemctl disable epcaldash.service
4. Logs
journalctl -u epcaldash.service
journalctl -u epcaldash.service -n 1000
With this setup, your Streamlit app runs as a managed service and will restart automatically if it fails.