#!/bin/bash # Health check and auto-restart script for QBPOS Help site DOMAIN="quickbookposhelp.access.ly" LOG_FILE="/var/log/qbpos-health-check.log" # Function to log messages log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } # Check if nginx is running if ! systemctl is-active --quiet nginx; then log_message "ERROR: Nginx is not running. Attempting to start..." systemctl start nginx if [ $? -eq 0 ]; then log_message "SUCCESS: Nginx restarted successfully" else log_message "CRITICAL: Failed to restart Nginx" exit 1 fi fi # Check if the site responds HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -m 10 "https://$DOMAIN" 2>/dev/null) if [ "$HTTP_CODE" != "200" ]; then log_message "WARNING: Site returned HTTP $HTTP_CODE. Reloading nginx..." systemctl reload nginx sleep 2 # Check again HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -m 10 "https://$DOMAIN" 2>/dev/null) if [ "$HTTP_CODE" = "200" ]; then log_message "SUCCESS: Site recovered after reload" else log_message "ERROR: Site still not responding properly (HTTP $HTTP_CODE)" fi else log_message "OK: Site is healthy (HTTP $HTTP_CODE)" fi