#!/bin/bash CHECK="✓"
CROSS="✗" # --- Configuration ---
SERVICE="nginx" # Change to your -weight: 500;">service name
LOG_FILE="/var/log/-weight: 500;">service-watchdog.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
NOTIFY_EMAIL="" # Optional: [email protected] # --- Check if -weight: 500;">service is running ---
if -weight: 500;">systemctl is-active --quiet "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE is running"
else echo "$CROSS [$DATE] $SERVICE is NOT running — attempting -weight: 500;">restart..." echo "$CROSS [$DATE] $SERVICE DOWN — restarting" >> "$LOG_FILE" # --- Attempt -weight: 500;">restart --- if -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE restarted successfully" | tee -a "$LOG_FILE" # --- Optional: send email notification --- if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE was down and has been restarted on $(hostname) at $DATE" \ | mail -s "[RECOVERED] $SERVICE restarted" "$NOTIFY_EMAIL" fi else echo "$CROSS [$DATE] $SERVICE FAILED to -weight: 500;">restart — manual intervention needed" \ | tee -a "$LOG_FILE" if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE failed to -weight: 500;">restart on $(hostname) at $DATE. Check: journalctl -u $SERVICE" \ | mail -s "[CRITICAL] $SERVICE -weight: 500;">restart failed" "$NOTIFY_EMAIL" fi fi
fi
#!/bin/bash CHECK="✓"
CROSS="✗" # --- Configuration ---
SERVICE="nginx" # Change to your -weight: 500;">service name
LOG_FILE="/var/log/-weight: 500;">service-watchdog.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
NOTIFY_EMAIL="" # Optional: [email protected] # --- Check if -weight: 500;">service is running ---
if -weight: 500;">systemctl is-active --quiet "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE is running"
else echo "$CROSS [$DATE] $SERVICE is NOT running — attempting -weight: 500;">restart..." echo "$CROSS [$DATE] $SERVICE DOWN — restarting" >> "$LOG_FILE" # --- Attempt -weight: 500;">restart --- if -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE restarted successfully" | tee -a "$LOG_FILE" # --- Optional: send email notification --- if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE was down and has been restarted on $(hostname) at $DATE" \ | mail -s "[RECOVERED] $SERVICE restarted" "$NOTIFY_EMAIL" fi else echo "$CROSS [$DATE] $SERVICE FAILED to -weight: 500;">restart — manual intervention needed" \ | tee -a "$LOG_FILE" if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE failed to -weight: 500;">restart on $(hostname) at $DATE. Check: journalctl -u $SERVICE" \ | mail -s "[CRITICAL] $SERVICE -weight: 500;">restart failed" "$NOTIFY_EMAIL" fi fi
fi
#!/bin/bash CHECK="✓"
CROSS="✗" # --- Configuration ---
SERVICE="nginx" # Change to your -weight: 500;">service name
LOG_FILE="/var/log/-weight: 500;">service-watchdog.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
NOTIFY_EMAIL="" # Optional: [email protected] # --- Check if -weight: 500;">service is running ---
if -weight: 500;">systemctl is-active --quiet "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE is running"
else echo "$CROSS [$DATE] $SERVICE is NOT running — attempting -weight: 500;">restart..." echo "$CROSS [$DATE] $SERVICE DOWN — restarting" >> "$LOG_FILE" # --- Attempt -weight: 500;">restart --- if -weight: 600;">sudo -weight: 500;">systemctl -weight: 500;">start "$SERVICE"; then echo "$CHECK [$DATE] $SERVICE restarted successfully" | tee -a "$LOG_FILE" # --- Optional: send email notification --- if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE was down and has been restarted on $(hostname) at $DATE" \ | mail -s "[RECOVERED] $SERVICE restarted" "$NOTIFY_EMAIL" fi else echo "$CROSS [$DATE] $SERVICE FAILED to -weight: 500;">restart — manual intervention needed" \ | tee -a "$LOG_FILE" if [ -n "$NOTIFY_EMAIL" ]; then echo "$SERVICE failed to -weight: 500;">restart on $(hostname) at $DATE. Check: journalctl -u $SERVICE" \ | mail -s "[CRITICAL] $SERVICE -weight: 500;">restart failed" "$NOTIFY_EMAIL" fi fi
fi
* * * * * /home/user/-weight: 500;">service-watchdog.sh >> /var/log/watchdog-cron.log 2>&1
* * * * * /home/user/-weight: 500;">service-watchdog.sh >> /var/log/watchdog-cron.log 2>&1
* * * * * /home/user/-weight: 500;">service-watchdog.sh >> /var/log/watchdog-cron.log 2>&1
-weight: 600;">sudo visudo
# Add this line:
youruser ALL=(ALL) NOPASSWD: /bin/-weight: 500;">systemctl -weight: 500;">start nginx
-weight: 600;">sudo visudo
# Add this line:
youruser ALL=(ALL) NOPASSWD: /bin/-weight: 500;">systemctl -weight: 500;">start nginx
-weight: 600;">sudo visudo
# Add this line:
youruser ALL=(ALL) NOPASSWD: /bin/-weight: 500;">systemctl -weight: 500;">start nginx
SERVICES=("nginx" "mysql" "redis-server")
for SERVICE in "${SERVICES[@]}"; do # ... same check logic ...
done
SERVICES=("nginx" "mysql" "redis-server")
for SERVICE in "${SERVICES[@]}"; do # ... same check logic ...
done
SERVICES=("nginx" "mysql" "redis-server")
for SERVICE in "${SERVICES[@]}"; do # ... same check logic ...
done - nginx — the web server
- mysql or mariadb — the database
- -weight: 500;">docker — the container daemon
- Custom services: my-node-app.-weight: 500;">service, redis-server, postgresql - Monitor CPU & RAM Usage — catches the OOM conditions that kill services in the first place
- Send Email Alert from Bash — the email sending setup if you've never configured mail on Linux