WatchDog script for checking server running

# 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/ 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
# about the restart.
# */5 * * * * sh /root/ mysqld mysql > /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`
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
#Check if we have have a second param
RESTART="/sbin/service ${SERVICE_NAME} restart" #No second param
RESTART="/sbin/service ${SERVICE_RESTARTNAME} restart" #Second param
#if we get no pids, service is not running
if [ "$pids" == "" ]
if [ -z $MAIL_TO ]
echo "$DATE : ${SERVICE_NAME} restarted - no email report configured."
echo "$DATE : Performing restart of ${SERVICE_NAME}" | mail -s "Service failure: ${SERVICE_NAME}" ${MAIL_TO}
echo "$DATE : Service ${SERVICE_NAME} is still working!"
# copylefted from
/bin/bash /root/ mysqld mysqld "$mailto"
/bin/bash /root/ httpd httpd "$mailto"
/bin/bash /root/ pound pound "$mailto"
/bin/bash /root/ redis-server redis "$mailto"
/bin/bash /root/ memcached memcached "$mailto"

/bin/bash /root/ searchd searchd "$mailto"

0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
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: How to rebuild and/or patch a RPM package
Viewed 5567 times since Sat, Jun 2, 2018
Top 4 Reasons for Node Reboot or Node Eviction in Real Application Cluster (RAC) Environment
Viewed 17108 times since Thu, Jun 21, 2018
How To Use Systemctl to Manage Systemd Services and Units
Viewed 5198 times since Mon, Dec 7, 2020
Linux Network (TCP) Performance Tuning with Sysctl
Viewed 7460 times since Fri, Aug 3, 2018
Automatic YUM Updates with Yum-cron
Viewed 9522 times since Fri, Oct 26, 2018
RHEL: Enabling standard ftp/telnet
Viewed 1601 times since Sun, May 27, 2018
Top 25 Best Linux Performance Monitoring and Debugging Tools
Viewed 4121 times since Sun, Sep 30, 2018
RHCS: Install a two-node basic cluster
Viewed 7824 times since Sun, Jun 3, 2018
Linux File Systems (mkfs, mount, fstab) ext4
Viewed 1469 times since Sat, Jun 2, 2018
How log rotation works with logrotate
Viewed 2570 times since Fri, Nov 30, 2018