#!/bin/bash
SERVER=$(hostname)
# E-mail address you want to send restart failures to
EMAIL="
[email protected]"
# Directory where script is located (no trailing slash)
SCRIPT_PATH="/path/to/script/dir"
# Log file that you want to capture events in
STATUS_LOG="$SCRIPT_PATH/status.log"
# Services you want to monitor
SERVICES="httpd mysqld postgresql qmail psa"
echo "Service Report for $SERVER" > $STATUS_LOG
echo "===========================================" 1>> $STATUS_LOG
echo "Run on $(date)" 1>> $STATUS_LOG
echo "===========================================" 1>> $STATUS_LOG
for i in $SERVICES; do
# Check the status of the services in the list
STATUS=$(/sbin/service $i status | sed -re 's/(pid|\(|\)|[0-9]{2,}|\.\.\.|\ )//g;s/\is/ : /g')
echo "[*] $STATUS" 1>> $STATUS_LOG
# If the string "running" could not be found, the service is assumed stopped
if [[ ! $STATUS == *running ]]; then
echo " - $i found to be not running. Restarting $i..." 1>> $STATUS_LOG
# Attempt to start the service
/sbin/service $i restart
# Make sure that the service was actually re-started.
NEW_STATUS=$(/sbin/service $i status | sed -re 's/(pid|\(|\)|[0-9]{2,}|\.\.\.|\ )//g;s/\is/ : /g')
if [[ ! $NEW_STATUS == *running ]]; then
echo " [x] $i could not be restarted. Please contact Support." 1>> $STATUS_LOG
else
echo " [*] $i has been restarted successfully." 1>> $STATUS_LOG
fi
cat $STATUS_LOG | mail -s "Service Monitor ($SERVER): $i FAILED" $EMAIL
fi
done
exit 0