WatchDog watchdog.sh script for checking server running

#!/bin/bash
# Service watchdog script
# Put in crontab to automatially restart services (and optionally email you) if they die for some reason.
# Note: You need to run this as root otherwise you won't be able to restart services.
#
# Example crontab usage:
#
# Strict check for apache2 service every 5 minutes, pipe results to /dev/null
# */5 * * * * sh /root/watchdog.sh apache2 "" > /dev/null
#
# "Loose" check for mysqld every 5 minutes, second parameter is the name of the service
# to restart, in case the application and service names differ. Also emails a report to admin@domain.com
# about the restart.
# */5 * * * * sh /root/watchdog.sh mysqld mysql admin@domain.com > /dev/null
 
# Common daemon names:
# Apache:
# apache2 - Debian/Ubuntu
# httpd - RHEL/CentOS/Fedora
# ---
# MySQL:
# mysql - Debian/Ubuntu
# mysqld - RHEL/CentOS/Fedora
# ---
# Service name
DATE=`date +%Y-%m-%d--%H-%M-%S`
SERVICE_NAME="$1"
SERVICE_RESTARTNAME="$2"
EXTRA_PGREP_PARAMS="-x" #Extra parameters to pgrep, for example -x is good to do exact matching
MAIL_TO="$3" #Email to send restart notifications to
 
#path to pgrep command, for example /usr/bin/pgrep
PGREP="pgrep"
 
#Check if we have have a second param
if [ -z $SERVICE_RESTARTNAME ]
then
RESTART="/sbin/service ${SERVICE_NAME} restart" #No second param
else
RESTART="/sbin/service ${SERVICE_RESTARTNAME} restart" #Second param
fi
 
pids=`$PGREP ${EXTRA_PGREP_PARAMS} ${SERVICE_NAME}`
 
#if we get no pids, service is not running
if [ "$pids" == "" ]
then
$RESTART
if [ -z $MAIL_TO ]
then
echo "$DATE : ${SERVICE_NAME} restarted - no email report configured."
else
echo "$DATE : Performing restart of ${SERVICE_NAME}" | mail -s "Service failure: ${SERVICE_NAME}" ${MAIL_TO}
fi
else
echo "$DATE : Service ${SERVICE_NAME} is still working!"
fi
 
# copylefted from https://gist.github.com/vodolaz095/5073080
 
 
#!/bin/bash
mailto="mymail@mydomain"
 
/bin/bash /root/watchdog.sh mysqld mysqld "$mailto"
/bin/bash /root/watchdog.sh httpd httpd "$mailto"
/bin/bash /root/watchdog.sh pound pound "$mailto"
/bin/bash /root/watchdog.sh redis-server redis "$mailto"
/bin/bash /root/watchdog.sh memcached memcached "$mailto"

/bin/bash /root/watchdog.sh searchd searchd "$mailto"

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
RHEL: Multipathing basics
Viewed 9313 times since Sat, Jun 2, 2018
stunnel Securing telnet connections with stunnel
Viewed 1576 times since Sun, Dec 6, 2020
red hat 7 tmpfiles service
Viewed 1893 times since Thu, Oct 11, 2018
6 rsync Examples to Exclude Multiple Files and Directories using exclude-from
Viewed 5039 times since Wed, Oct 31, 2018
RHCS6: Debug and test multicast traffic between two hosts
Viewed 6763 times since Sun, Jun 3, 2018
bash mistakes This page is a compilation of common mistakes made by bash users. Each example is flawed in some way.
Viewed 9179 times since Sun, Dec 6, 2020
Jak wygenerować silne hasła jednorazowe w Linuksie?
Viewed 2409 times since Thu, May 24, 2018
How To Use Systemctl to Manage Systemd Services and Units
Viewed 7598 times since Mon, Dec 7, 2020
RHEL: Services basic management - chkconfig
Viewed 5749 times since Sat, Jun 2, 2018
YUM How to use yum command on CentOS/RHEL
Viewed 7277 times since Thu, Oct 25, 2018