Cron Script to Restart Apache/MySQL on Crash (Centos/RHEL)

If you need to auto restart Apache and MySQL in case one or both are crashed, just schedule this SH script on Centos (RHEL) in your cron section.

Save the code section below as restart_apache_mysql.sh in your home directory.
Give the file permissions, so root can execute the script and schedule it in Cron.

#!/bin/bash
# APACHE SECTION
RESTART="/etc/rc.d/init.d/httpd restart"
PGREP="/usr/bin/pgrep"
HTTPD="httpd"
$PGREP ${HTTPD}
if [ $? -ne 0 ]
then
$RESTART
fi
# MYSQL SECTION
RESTARTM="/etc/rc.d/init.d/mysqld restart"
MYSQLD="mysqld"
$PGREP ${MYSQLD}
if [ $? -ne 0 ]
then
$RESTARTM
fi

 

 

In Webmin, you can schedule above script to run every minute, as follows:

Enjoy.



Facebook Comments