43 lines
1.4 KiB
Bash
Executable File
43 lines
1.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# Tailscale health check and auto-recovery script
|
|
# Run this via cron every 5-10 minutes
|
|
|
|
CONTAINER_NAME="${CONTAINER_NAME:-tailscale}"
|
|
MAX_RESTART_ATTEMPTS=3
|
|
RESTART_COUNT_FILE="/tmp/tailscale_restart_count"
|
|
|
|
# Check if container is running
|
|
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
|
|
echo "$(date): Container not running, attempting to start..."
|
|
docker start "${CONTAINER_NAME}"
|
|
sleep 10
|
|
fi
|
|
|
|
# Check Tailscale connection status
|
|
if ! docker exec "${CONTAINER_NAME}" tailscale status &>/dev/null; then
|
|
echo "$(date): Tailscale not connected properly"
|
|
|
|
# Track restart attempts
|
|
if [ -f "$RESTART_COUNT_FILE" ]; then
|
|
COUNT=$(cat "$RESTART_COUNT_FILE")
|
|
else
|
|
COUNT=0
|
|
fi
|
|
|
|
if [ "$COUNT" -lt "$MAX_RESTART_ATTEMPTS" ]; then
|
|
echo "$(date): Restart attempt $((COUNT + 1)) of $MAX_RESTART_ATTEMPTS"
|
|
docker restart "${CONTAINER_NAME}"
|
|
echo $((COUNT + 1)) > "$RESTART_COUNT_FILE"
|
|
|
|
# Wait and try to reconnect
|
|
sleep 30
|
|
docker exec "${CONTAINER_NAME}" tailscale up --authkey="${TAILSCALE_AUTH_KEY}" 2>/dev/null || true
|
|
else
|
|
echo "$(date): Max restart attempts reached. Manual intervention needed."
|
|
# Could send alert here
|
|
fi
|
|
else
|
|
# Connection is good, reset counter
|
|
[ -f "$RESTART_COUNT_FILE" ] && rm "$RESTART_COUNT_FILE"
|
|
echo "$(date): Tailscale is healthy"
|
|
fi |