#!/usr/bin/env sh# Script Name: nginx_maintenance.sh# Author: Aina# Data Created: 2025-08-25 | Data Modified: 2025-09-08# Exit immediately if a command exits with a non-zero status# Treat unset variables as an error and exit immediatelyset-eu
# Check the permissionif[$(id-u)-ne0];thenecho'This script must be run with `sudo`.'exit1fi# Define variableslog_dir="/var/log/nginx"current_date="$(date-I)"report_dir="${HOME}/maintenance_reports"report_file="${report_dir}/log_${current_date}.log"website_dir="/var/www/html"mkdir-p"${report_dir}"print_head(){head_text="Outputs of the command \`${1}':"echo"${head_text}"printf"%0.s-"$(seq1$(exprlength"${head_text}"))echo"\n\n\`\`\` sh"}command_exist(){command-v"${1}">/dev/null2>&1echo"${1} processing ..."}check_status(){ifcommand_exist${1};thenprint_head"${1}">>"${report_file}"eval"${1}">>"${report_file}"echo"\`\`\`\n">>"${report_file}"elseecho"Command \`${1}' not found.">>"${report_file}"fi}# Check system resources and disk usagecheck_status'uptime'check_status'free -h'check_status'dmesg | tail -n 20'check_status'vmstat 1 4'check_status'df -h'check_status'du -h -d 1 "${website_dir}" | sort -rh | head -n 10'# Check process statuscheck_status'ps aux | grep [n]ginx'check_status'ps aux --sort=%cpu | head -n 10'check_status'ps aux --sort=%mem | head -n 10'# Check network statuscheck_status'ss -s'check_status'ss -tunlp'# Performance monitoring commands# check_status 'iostat -x 1 3'# check_status 'sar -u 1 3'# check_status 'sar -r 1 3'