WatchDog watchdog.sh script for checking server running
Article Number: 344 | Rating: Unrated | Last Updated: Tue, Jul 31, 2018 9:37 AM
| # 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. |
| # 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 |
| # */5 * * * * sh /root/watchdog.sh mysqld mysql admin@domain.com > /dev/null |
| # apache2 - Debian/Ubuntu |
| # httpd - RHEL/CentOS/Fedora |
| # mysqld - RHEL/CentOS/Fedora |
| 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 |
| if [ -z $SERVICE_RESTARTNAME ] |
| RESTART="/sbin/service ${SERVICE_NAME} restart" #No second param |
| RESTART="/sbin/service ${SERVICE_RESTARTNAME} restart" #Second param |
| pids=`$PGREP ${EXTRA_PGREP_PARAMS} ${SERVICE_NAME}` |
| #if we get no pids, service is not running |
| 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 https://gist.github.com/vodolaz095/5073080 |
| /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"
Posted - Tue, Jul 31, 2018 9:37 AM. This article has been viewed 6182 times.
Filed Under:
Linux
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Related Articles

Testing TLS/SSL encryption
Viewed 14140 times since Thu, Jan 16, 2020
Epoch & Unix Timestamp Conversion Tools
Viewed 77928 times since Fri, Jun 22, 2018
python learning
Viewed 2534 times since Wed, Dec 18, 2019
Using grep to find string in files
Viewed 3141 times since Fri, May 15, 2020
Super Grub2 Disk
Viewed 4152 times since Wed, May 22, 2019
Logrotate Example for Custom Logs
Viewed 3259 times since Sun, Jan 12, 2020