Overview
The purpose of this bash script is to monitor a specific service that
needs to be running at all times. In case the service is not running, the
script will restart the service and send out notification to the recipients.
Applies To
Tested on RHEL 7, CentOS 7
Pre-requisites
·
mailx package has to be installed
Bash Script Snippet
#!/bin/bash
#
# This script check’s the status
of the service, restart and notify Recipients
#
# Environment Variables
#
HOST_NAME=`hostname`
SERVICE_NAME=httpd.service
EMAIL_TO=RecipentsMailAddress@domain.com
ExecutedBy=`who am i`
#
# Verify if the service status is active or inactive state
#
if [ `systemctl is-active ${SERVICE_NAME}` != "active" ]
then
echo "$SERVICE_NAME is
not running on $HOST_NAME restarting it"
systemctl restart
${SERVICE_NAME}
Exit_Code=`echo $?`
if [ ${Exit_Code} =
"0" ]; then
# Send EMail Notification
mail -s "${SERVICE_NAME}
was restarted on ${HOST_NAME}" ${EMAIL_TO} <<<
"${SERVICE_NAME} was restarted on ${HOST_NAME} run by ${ExecutedBy}"
exit 0
fi
fi
exit 0
#
# End of script
Execute Script
This script can be run in 2 ways; manually and via cronjob
First step is to add execute permission to the shell script, only once.
chmod +x /usr/bin/RestartServiceIfDown
Execute Script - Manually
To run the script manually, run the command;
/usr/bin/RestartServiceIfDown
Execute Script - Cronjob
You can alternatively, add this script to a cronjob to check as service
state at regular intervals.
In order monitor the service with cronjob, add a crontab entry.
crontab snippet
# Check httpd.service Status and restart if inactive
*/15 * * * * /usr/bin/RestartServiceIfDown > /dev/null
Now every 15 minutes the script should run, check the service is
running and if not restart it for you.
Note:
1)
Email Notification will be sent only upon
restarting of service
2)
If the service is restarting frequently, you
need to verify the cause and fix the issue accordingly
E-Mail Notification
E-Mail notification will be sent on both the methods of execution.
Slideshare Information
Guide to utilize the bash script and configure is uploaded with screenshots.
An other option is to use the Restart and FailureAction options in the systemd unit file of a service.
ReplyDeleteThe first option defines if a given process needs to be restarted when failing.
The second option executes a specified action when a failure of the given process is detected.
Thanks for the information, will try to incorporate.
Delete