Skip to content

Instantly share code, notes, and snippets.

@crh0831
Created April 14, 2025 17:57
Show Gist options
  • Save crh0831/739c0db4d1ede40842925374b68d849b to your computer and use it in GitHub Desktop.
Save crh0831/739c0db4d1ede40842925374b68d849b to your computer and use it in GitHub Desktop.
Apache OOM Restart

A rube-goldberg style solution for restarting Apache when it gets killed my the OOM-Killer and logging the results for further analysis

#!/usr/bin/env bash
#===============================================================================
#
#          FILE: apache-restart.sh
#         USAGE:
#   DESCRIPTION: Watches syslog for OOM message, restarts Apache if killed
#         NOTES: The downside to this script is that it'll fire for the entire
#                hour that the OOM string appears in syslog. But it'll check for 
#                Apache's status before actually doing anything
#        AUTHOR: C Hawley
#       CREATED: 2024-10-23
#       UPDATED: 2024-10-23
#
#===============================================================================
 
set -o nounset                               # Treat unset variables as an error
 
##################################################################
# Purpose: Send output to screen and specified file
# Requires: define to `logfile` variable to send out put to logfile
# Usage: logecho "Log Message"
##################################################################
logfile=/tmp/logchaser.log
logecho() {
    echo "${1}"
    if [[ ! -z "${logfile}" ]]; then
        echo "${1}" >> "${logfile}"
    fi
}
 
# Log to search
target_log='/var/log/syslog'
# String to watch for
target_param1='apache2.service: A process of this unit has been killed by the OOM killer.'
 
# Look for current hour of todays date
# ex: 'Oct 23 14'
target_date=$(date '+%b %d %H')
 
# If the OOM string is found in the last hour's logs, then check on the status of Apache
# If Apache is not active, restart it and log the time
# If Apache is active, do nothing and log the time
if [[ $(grep "${target_date}" "${target_log}" | grep "${target_param1}") ]]; then
	if ! systemctl is-active apache2; then 
		logecho "$(date '+%Y-%m-%d %H:%M') OOM found in hourly log. Apache is dead. Restarting"
		systemctl restart apache2
	else
		logecho "$(date '+%Y-%m-%d %H:%M') OOM found in hourly log, but Apache is running. No action taken"
	fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment