BASH

Monitor and Restart a Linux Service

Keep your services running reliably. This bash script checks the status of a specified systemd service and restarts it if it's found to be inactive or failed, ensuring uptime and stability.

#!/bin/bash

# Configuration
SERVICE_NAME="nginx" # Name of the systemd service to monitor (e.g., apache2, postgresql, custom-app)
LOG_FILE="/var/log/service_monitor.log" # Path for logging script actions

# Function to log messages with a timestamp
log_message() {
  echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

log_message "Starting service monitor for '$SERVICE_NAME'..."

# Check if the service is active
# 'systemctl is-active --quiet' returns 0 if active, non-zero otherwise
if systemctl is-active --quiet "$SERVICE_NAME"; then
  log_message "Service '$SERVICE_NAME' is active and running."
else
  log_message "Service '$SERVICE_NAME' is inactive or failed. Attempting to restart..."
  
  # Attempt to restart the service using sudo
  # Ensure the user running this script has sudo privileges for systemctl restart
  if sudo systemctl restart "$SERVICE_NAME"; then
    # Give it a moment to start
    sleep 5
    if systemctl is-active --quiet "$SERVICE_NAME"; then
      log_message "Service '$SERVICE_NAME' restarted successfully."
    else
      log_message "Error: Service '$SERVICE_NAME' failed to restart after attempt." >&2
    fi
  else
    log_message "Error: Failed to execute 'sudo systemctl restart $SERVICE_NAME'. Check sudo permissions or service name." >&2
  fi
fi

log_message "Service monitor finished for '$SERVICE_NAME'."
How it works: This bash script is designed to ensure the continuous operation of critical Linux services. It periodically checks the status of a specified systemd service (e.g., Nginx, Apache, PostgreSQL) using `systemctl is-active --quiet`. If the service is found to be inactive or in a failed state, the script attempts to restart it using `sudo systemctl restart`. All actions, including status checks and restart attempts, are logged to a designated file, providing a clear audit trail and aiding in troubleshooting service interruptions. This is a common pattern for simple service health checks.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs