This commit is contained in:
2026-08-12 18:22:26 -05:00
parent 7e3b3dff6d
commit 9cc7093c48
6 changed files with 102 additions and 39 deletions
+31 -1
View File
@@ -133,6 +133,7 @@ cleanup() {
kill "$pid" 2>/dev/null || true
fi
rm -f "$pidfile"
rm -f "$PID_DIR/$name.cmd"
fi
done
fi
@@ -174,6 +175,7 @@ start_go_service() {
echo -e "${GREEN}Starting $name...${NC}"
(cd "$dir" && ./"$binary" > "$LOG_DIR/$name.log" 2>&1) &
echo $! > "$PID_DIR/$name.pid"
echo "(cd \"$dir\" && .\"/$binary\" > \"$LOG_DIR/$name.log\" 2>&1) &" > "$PID_DIR/$name.cmd"
# Brief wait to check if it started successfully
sleep 1
@@ -203,6 +205,7 @@ start_python_service() {
# Activate virtual environment and run script
(source .venv/bin/activate && python3 "$script" $config_arg > "$LOG_DIR/$label.log" 2>&1) &
echo $! > "$PID_DIR/$label.pid"
echo "(source .venv/bin/activate && python3 \"$script\" $config_arg > \"$LOG_DIR/$label.log\" 2>&1) &" > "$PID_DIR/$label.cmd"
# Brief wait to check if it started successfully
sleep 1
@@ -324,6 +327,32 @@ echo ""
echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}"
echo ""
# Function to restart a dead service from its saved relaunch command
restart_service() {
local name=$1
local cmd_file="$PID_DIR/$name.cmd"
if [ ! -f "$cmd_file" ]; then
echo -e "${RED}$name: no restart command stored, cannot auto-restart${NC}"
return 1
fi
echo -e "${YELLOW}Restarting $name...${NC}"
rm -f "$PID_DIR/$name.pid"
# shellcheck disable=SC2046
eval "$(cat "$cmd_file")"
echo $! > "$PID_DIR/$name.pid"
sleep 1
if kill -0 $(cat "$PID_DIR/$name.pid") 2>/dev/null; then
echo -e "${GREEN}$name restarted (PID: $(cat "$PID_DIR/$name.pid"))${NC}"
return 0
else
echo -e "${RED}$name failed to restart (check $LOG_DIR/$name.log)${NC}"
return 1
fi
}
# Keep script running and monitor processes
while true; do
# Check if all processes are still running
@@ -335,12 +364,13 @@ while true; do
name=$(basename "$pidfile" .pid)
echo -e "${RED}Warning: $name (PID: $pid) has stopped unexpectedly${NC}"
all_running=false
restart_service "$name"
fi
fi
done
if [ "$all_running" = false ]; then
echo -e "${YELLOW}Some services have stopped. Check logs in $LOG_DIR${NC}"
echo -e "${YELLOW}Some services were restarted. Check logs in $LOG_DIR${NC}"
fi
sleep 10