#!/bin/bash
# URL of Apache server-status
STATUS_URL="http://localhost/server-status?auto"
# Get current number of busy workers
HTML_STATUS_FILE="/tmp/cron_html_status.html"
curl -s "$STATUS_URL" > "$HTML_STATUS_FILE"
BUSY_WORKERS=$(cat "$HTML_STATUS_FILE" | grep "BusyWorkers" | awk '{print $2}')
# Restart Apache if busy workers exceed threshold
THRESHOLD=200
if [ "$BUSY_WORKERS" -gt "$THRESHOLD" ]; then
MESSAGE="$(date): Restarting Apache - BusyWorkers = $BUSY_WORKERS"
echo "$MESSAGE" >> /var/log/apache_monitor.log
cp "$HTML_STATUS_FILE" "/var/log/apache_monitor_html_$(date).log"
curl -s "http://localhost/server-status" > "$HTML_STATUS_FILE"."$(date '+%Y-%m-%d-%H-%M-%S').html"
logger -t apache_monitor "$MESSAGE"
systemctl restart apache2
fi
A simple cronjobs to restart your apache server when it’s too busy
2025-10-18