forked from KPR/KPRSpaceAPI
79 lines
2.4 KiB
Fish
79 lines
2.4 KiB
Fish
|
|
#!/usr/bin/env fish
|
||
|
|
# Run the app in web mode with recommended env vars (fish shell)
|
||
|
|
# Usage: ./run_web.fish
|
||
|
|
|
||
|
|
# create venv if missing
|
||
|
|
if not test -d venv
|
||
|
|
python3 -m venv venv
|
||
|
|
end
|
||
|
|
|
||
|
|
# activate venv
|
||
|
|
source venv/bin/activate.fish
|
||
|
|
|
||
|
|
# ensure dependencies
|
||
|
|
pip install -r requirements.txt
|
||
|
|
|
||
|
|
# recommended env vars (adjust if needed)
|
||
|
|
set -x SNAPSHOT_INTERVAL 600 # seconds (600 = 10 minutes)
|
||
|
|
set -x BRIGHTNESS_THRESHOLD 50 # 0-255
|
||
|
|
set -x AUTO_START false # true to auto-start monitor inside web server
|
||
|
|
# ensure CONSOLE_MODE is unset so the Flask server will run
|
||
|
|
set -e CONSOLE_MODE
|
||
|
|
|
||
|
|
# logging
|
||
|
|
set -x LOG_DIR logs
|
||
|
|
mkdir -p $LOG_DIR
|
||
|
|
set -x LOG_FILE $LOG_DIR/run_web.log
|
||
|
|
set -x PID_FILE $LOG_DIR/run_web.pid
|
||
|
|
|
||
|
|
# If a previous PID file exists, try to stop that process first
|
||
|
|
if test -f $PID_FILE
|
||
|
|
set OLD_PID (cat $PID_FILE)
|
||
|
|
if test -n "$OLD_PID"
|
||
|
|
if ps -p $OLD_PID > /dev/null 2>&1
|
||
|
|
echo "Stopping previous process pid $OLD_PID"
|
||
|
|
kill $OLD_PID 2>/dev/null || kill -9 $OLD_PID 2>/dev/null
|
||
|
|
sleep 1
|
||
|
|
end
|
||
|
|
rm -f $PID_FILE
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
# Also check for any process listening on port 5000 and kill it to free the port
|
||
|
|
if type lsof >/dev/null 2>&1
|
||
|
|
set PORT_PIDS (lsof -ti:5000 2>/dev/null)
|
||
|
|
if test -n "$PORT_PIDS"
|
||
|
|
echo "Killing processes on port 5000: $PORT_PIDS"
|
||
|
|
for p in $PORT_PIDS
|
||
|
|
kill $p 2>/dev/null || kill -9 $p 2>/dev/null
|
||
|
|
end
|
||
|
|
end
|
||
|
|
else
|
||
|
|
# fallback using ss and awk if lsof not available
|
||
|
|
if type ss >/dev/null 2>&1
|
||
|
|
for line in (ss -ltnp 2>/dev/null | grep ':5000' | awk '{print $NF}' | sed -n 's/.*pid=\([0-9]*\).*/\1/p')
|
||
|
|
if test -n "$line"
|
||
|
|
echo "Killing process pid $line listening on port 5000"
|
||
|
|
kill $line 2>/dev/null || kill -9 $line 2>/dev/null
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
echo "Starting web app in background; logs: $LOG_FILE"
|
||
|
|
# Use bash to ensure POSIX redirection works reliably
|
||
|
|
bash -lc "python app.py > '$LOG_FILE' 2>&1 & echo \$! > '$PID_FILE'"
|
||
|
|
|
||
|
|
# wait briefly for process to start
|
||
|
|
sleep 1
|
||
|
|
if test -f $PID_FILE
|
||
|
|
set PID (cat $PID_FILE)
|
||
|
|
echo "Started pid: $PID"
|
||
|
|
else
|
||
|
|
echo "Failed to start process; see $LOG_FILE for details"
|
||
|
|
end
|
||
|
|
|
||
|
|
# tail the log so the user can see live output (Ctrl+C to stop tail but process keeps running)
|
||
|
|
echo "Tailing log (press Ctrl+C to stop tail, process will continue running in background)."
|
||
|
|
tail -n 200 -f $LOG_FILE
|