Quantcast
Channel: myVesta
Viewing all articles
Browse latest Browse all 200

Bash • Script for checking inactive Services

$
0
0
I have developed a bash script that monitors the running services in myVestaCP. Should any service cease to function, the script will restart it after two minutes. It operates effectively. However, for safety reasons, it is advisable to test it on a non-production service or to create a snapshot of your VPS prior to usage.

Code:

#!/bin/bash# Comprehensive Service Monitor and Recovery Script for Debian-based systems# This script sets up a systemd service to monitor and automatically restart critical services if they become inactive.# Services monitored: MariaDB, Apache2, Nginx, PHP7.4, Redis, Memcached# Ensure the script is run as rootif [ "$EUID" -ne 0 ]; then    echo "Please run as root"    exit 1fi# Define the path for the monitor scriptMONITOR_SCRIPT="/usr/local/bin/service_monitor.sh"# Define the path for the systemd service fileSERVICE_FILE="/etc/systemd/system/service-monitor.service"# Log file pathLOG_FILE="/var/log/service_monitor.log"# Create the monitor scriptcat << EOF > "$MONITOR_SCRIPT"#!/bin/bash# Log file pathLOG_FILE="/var/log/service_monitor.log"# Array of services to monitorSERVICES=("mariadb" "apache2" "nginx" "php7.4-fpm" "redis-server" "memcached")# Function to log messageslog_message() {    echo "\$(date '+%Y-%m-%d %H:%M:%S') - \$1" >> "\$LOG_FILE"}# Function to check and restart a servicecheck_and_restart_service() {    local service=\$1    if ! systemctl is-active --quiet "\$service"; then        log_message "Service \$service is inactive. Waiting 2 minutes before attempting restart."        sleep 120        if ! systemctl is-active --quiet "\$service"; then            log_message "Attempting to restart \$service"            if systemctl restart "\$service"; then                log_message "Service \$service restarted successfully"            else                log_message "Failed to restart \$service. Manual intervention may be required."            fi        else            log_message "Service \$service is now active. No restart needed."        fi    fi}# Main loopwhile true; do    for service in "\${SERVICES[@]}"; do        # Use a subshell to handle each service check concurrently        (            check_and_restart_service "\$service"        ) &    done    # Wait for all background processes to finish    wait    # Sleep for 2 minutes before the next check    sleep 120doneEOF# Make the monitor script executablechmod +x "$MONITOR_SCRIPT"# Create the systemd service filecat << EOF > "$SERVICE_FILE"[Unit]Description=Service Monitor and RecoveryAfter=network.target[Service]ExecStart=$MONITOR_SCRIPTRestart=alwaysUser=root[Install]WantedBy=multi-user.targetEOF# Reload systemd, enable and start the servicesystemctl daemon-reloadsystemctl enable service-monitor.servicesystemctl start service-monitor.serviceecho "Service monitor has been set up and started."echo "You can check its status with: systemctl status service-monitor"echo "Logs are available at: $LOG_FILE"

Statistics: Posted by shahidirfan — Sat Aug 10, 2024 12:05 pm



Viewing all articles
Browse latest Browse all 200

Trending Articles