#!/usr/bin/env bash # Keep the Streamlit app alive on port 8599. # # The server (or the machine) can drop it at any time, so every 5 minutes # this checks whether something is answering on the port and, if not, # relaunches it detached. Streamlit only hot-reloads app.py, so module # changes need this relaunch (or a manual one) to take effect. # # Usage: setsid nohup fundlab/server_watchdog.sh >fundlab/server_watchdog.log 2>&1 & set -u cd "$(dirname "$0")/.." PORT=8599 INTERVAL=300 alive() { curl -s -o /dev/null --max-time 8 "http://127.0.0.1:${PORT}/_stcore/health" \ && [ "$(curl -s --max-time 8 "http://127.0.0.1:${PORT}/_stcore/health")" = "ok" ] } while true; do if ! alive; then { date "+%Y-%m-%d %H:%M:%S" echo "server_watchdog: no answer on :${PORT} - relaunching" } >> fundlab/server_watchdog.log setsid nohup .venv/bin/streamlit run app.py \ --server.port "${PORT}" --server.headless true \ --browser.gatherUsageStats false >> fundlab/streamlit.log 2>&1 < /dev/null & sleep 15 fi sleep "${INTERVAL}" done