#!/bin/bash # Script to check nginx status and restore it if needed # This script can be run manually or via cron # Usage: ./check-and-restore-nginx.sh set -e SERVER_IP="147.45.152.129" SERVER_USER="root" echo "๐Ÿ” Checking nginx status and restoring if needed..." echo "Server: $SERVER_IP" echo "" ssh "$SERVER_USER@$SERVER_IP" << 'ENDSSH' set -e NGINX_STATUS=0 # Check if nginx is running if systemctl is-active --quiet nginx; then echo "โœ… Nginx is running" # Additional check: can nginx respond? if curl -f -s http://localhost/health > /dev/null 2>&1 || \ curl -f -s http://localhost > /dev/null 2>&1; then echo "โœ… Nginx is responding to requests" else echo "โš ๏ธ Nginx process is running but not responding to requests" NGINX_STATUS=1 fi else echo "โŒ Nginx is not running!" NGINX_STATUS=1 fi # If nginx is not working, try to restore it if [ $NGINX_STATUS -ne 0 ]; then echo "" echo "๐Ÿ”ง Attempting to restore nginx..." # Check nginx configuration echo "๐Ÿงช Testing nginx configuration..." if nginx -t 2>&1; then echo "โœ… Nginx configuration is valid" else echo "โŒ Nginx configuration has errors!" echo "๐Ÿ“‹ Recent nginx errors:" journalctl -u nginx --since "10min ago" --no-pager -n 20 || true exit 1 fi # Try to start nginx echo "" echo "๐Ÿš€ Starting nginx..." if systemctl start nginx; then sleep 2 if systemctl is-active --quiet nginx; then echo "โœ… Nginx started successfully" # Verify it's responding sleep 1 if curl -f -s http://localhost > /dev/null 2>&1; then echo "โœ… Nginx is responding to requests" else echo "โš ๏ธ Nginx started but not responding yet (may need a moment)" fi else echo "โŒ Failed to start nginx!" echo "๐Ÿ“‹ Recent nginx errors:" journalctl -u nginx --since "10min ago" --no-pager -n 30 || true exit 1 fi else echo "โŒ Failed to start nginx service!" echo "๐Ÿ“‹ Recent nginx errors:" journalctl -u nginx --since "10min ago" --no-pager -n 30 || true exit 1 fi fi # Check for OOM kills in system logs echo "" echo "๐Ÿ” Checking for OOM kills in system logs..." if dmesg | grep -i "oom\|killed process.*nginx" | tail -5; then echo "โš ๏ธ Found OOM kill events related to nginx!" echo "๐Ÿ’ก Consider checking system memory usage: free -h" else echo "โœ… No recent OOM kill events for nginx" fi # Show current memory usage echo "" echo "๐Ÿ“Š Current system memory usage:" free -h || true # Show nginx process info echo "" echo "๐Ÿ“Š Nginx process information:" if pgrep -f 'nginx: master' > /dev/null; then MASTER_PID=$(pgrep -f 'nginx: master' | head -1) echo " Master PID: $MASTER_PID" # Check OOM score if [ -f "/proc/$MASTER_PID/oom_score_adj" ]; then OOM_SCORE=$(cat "/proc/$MASTER_PID/oom_score_adj") echo " OOM Score Adjust: $OOM_SCORE" if [ "$OOM_SCORE" = "-1000" ]; then echo " โœ… Nginx is protected from OOM killer" else echo " โš ๏ธ Nginx OOM protection may not be configured correctly" fi fi # Show memory usage of nginx processes echo " Memory usage:" ps aux | grep -E 'nginx: (master|worker)' | grep -v grep || true else echo " โš ๏ธ Nginx master process not found" fi echo "" echo "โœ… Nginx check completed" ENDSSH echo "" echo "โœ… Nginx status check completed!"