120 lines
2.9 KiB
Bash
Executable File
120 lines
2.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# VizionStreamer Watchdog Script
|
|
# Automatically restarts the streamer if it crashes
|
|
# Usage: ./watchdog.sh
|
|
|
|
STREAMER_PATH="./build/vizionStreamer"
|
|
CHECK_INTERVAL=5 # Seconds between checks
|
|
LOG_FILE="./watchdog.log"
|
|
PID_FILE="./vizionStreamer.pid"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Function to log messages
|
|
log() {
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Function to start the streamer
|
|
start_streamer() {
|
|
log "${GREEN}Starting VizionStreamer...${NC}"
|
|
$STREAMER_PATH &
|
|
STREAMER_PID=$!
|
|
echo $STREAMER_PID > "$PID_FILE"
|
|
log "VizionStreamer started with PID: $STREAMER_PID"
|
|
}
|
|
|
|
# Function to check if process is running
|
|
is_running() {
|
|
if [ -f "$PID_FILE" ]; then
|
|
PID=$(cat "$PID_FILE")
|
|
if ps -p $PID > /dev/null 2>&1; then
|
|
return 0 # Running
|
|
fi
|
|
fi
|
|
return 1 # Not running
|
|
}
|
|
|
|
# Function to stop the streamer
|
|
stop_streamer() {
|
|
if [ -f "$PID_FILE" ]; then
|
|
PID=$(cat "$PID_FILE")
|
|
if ps -p $PID > /dev/null 2>&1; then
|
|
log "${YELLOW}Stopping VizionStreamer (PID: $PID)...${NC}"
|
|
kill $PID
|
|
sleep 2
|
|
# Force kill if still running
|
|
if ps -p $PID > /dev/null 2>&1; then
|
|
log "${RED}Force killing VizionStreamer...${NC}"
|
|
kill -9 $PID
|
|
fi
|
|
fi
|
|
rm -f "$PID_FILE"
|
|
fi
|
|
}
|
|
|
|
# Trap SIGINT and SIGTERM to cleanly shutdown
|
|
cleanup() {
|
|
log "${YELLOW}Watchdog shutting down...${NC}"
|
|
stop_streamer
|
|
log "${GREEN}Watchdog stopped${NC}"
|
|
exit 0
|
|
}
|
|
|
|
trap cleanup SIGINT SIGTERM
|
|
|
|
# Check if vizionStreamer executable exists
|
|
if [ ! -f "$STREAMER_PATH" ]; then
|
|
log "${RED}ERROR: VizionStreamer not found at $STREAMER_PATH${NC}"
|
|
log "Please build the project first: cmake --build build"
|
|
exit 1
|
|
fi
|
|
|
|
# Main watchdog loop
|
|
log "${GREEN}=== VizionStreamer Watchdog Started ===${NC}"
|
|
log "Check interval: ${CHECK_INTERVAL} seconds"
|
|
log "Press Ctrl+C to stop"
|
|
|
|
RESTART_COUNT=0
|
|
|
|
# Start streamer initially
|
|
start_streamer
|
|
sleep 2 # Give it time to start
|
|
|
|
while true; do
|
|
sleep $CHECK_INTERVAL
|
|
|
|
if ! is_running; then
|
|
RESTART_COUNT=$((RESTART_COUNT + 1))
|
|
log "${RED}VizionStreamer died! Restart count: $RESTART_COUNT${NC}"
|
|
|
|
# Optional: limit restart attempts
|
|
if [ $RESTART_COUNT -gt 10 ]; then
|
|
log "${RED}ERROR: Too many restarts (10). Giving up.${NC}"
|
|
log "Check the logs for recurring errors."
|
|
exit 1
|
|
fi
|
|
|
|
# Clean up old PID file
|
|
rm -f "$PID_FILE"
|
|
|
|
# Wait a bit before restarting to avoid rapid restart loops
|
|
sleep 2
|
|
|
|
# Restart
|
|
start_streamer
|
|
sleep 2
|
|
else
|
|
# Still running - reset restart counter on successful check
|
|
if [ $RESTART_COUNT -gt 0 ]; then
|
|
RESTART_COUNT=0
|
|
log "${GREEN}VizionStreamer stable again${NC}"
|
|
fi
|
|
fi
|
|
done
|