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