#!/bin/bash # # Minecraft Server Health Check Script # Deployed by Ansible - managed file # # Checks that the Minecraft server is running and responding. # set -euo pipefail SERVER_NAME="{{ minecraft.server_name }}" PORT="{{ minecraft.port }}" CONTAINER_NAME="{{ podman_container_name }}" # Colors GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' log() { echo -e "$(date '+%Y-%m-%d %H:%M:%S') $*" } # Check 1: Is the systemd service active? SERVICE_STATUS=$(systemctl is-active "${SERVER_NAME}" 2>/dev/null || echo "inactive") if [ "$SERVICE_STATUS" != "active" ]; then log "${RED}[FAIL]${NC} Systemd service ${SERVER_NAME} is not active (status: ${SERVICE_STATUS})" exit 1 fi log "${GREEN}[OK]${NC} Systemd service ${SERVER_NAME} is active" # Check 2: Is the container running? if ! podman ps --format '{% raw %}{{.Names}} {{.Status}}{% endraw %}' | grep -q "${CONTAINER_NAME}"; then log "${RED}[FAIL]${NC} Container ${CONTAINER_NAME} is not running" exit 1 fi log "${GREEN}[OK]${NC} Container ${CONTAINER_NAME} is running" # Check 3: Is the Minecraft port responding? # Minecraft responds to HTTP with 401 "It's a teapot" or similar if curl -s -o /dev/null -m 5 http://localhost:${PORT} 2>/dev/null; then log "${GREEN}[OK]${NC} Port ${PORT} is responding" elif curl -s -o /dev/null -m 5 -w "%{http_code}" http://localhost:${PORT} 2>/dev/null | grep -q "401"; then log "${GREEN}[OK]${NC} Port ${PORT} is responding (401 is expected for Minecraft)" else # Check if port is at least listening if ss -tln | grep -q ":${PORT}"; then log "${YELLOW}[WARN]${NC} Port ${PORT} is listening but not responding to HTTP (may be normal)" else log "${RED}[FAIL]${NC} Port ${PORT} is not listening" exit 1 fi fi # Check 4: Check recent logs for errors LOG_TAIL=$(journalctl -u "${SERVER_NAME}" --no-pager -n 50 2>/dev/null | grep -i "error\|fatal\|crash" | tail -n 5) if [ -n "$LOG_TAIL" ]; then log "${YELLOW}[WARN]${NC} Recent errors found in logs:" echo "$LOG_TAIL" else log "${GREEN}[OK]${NC} No recent errors in logs" fi log "${GREEN}Health check completed successfully${NC}" exit 0