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

April 6th, 2011

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.



Comments

comments

Entry Filed under: English Blog


Search

Calendar

April 2011
M T W T F S S
« Mar   May »
 123
45678910
11121314151617
18192021222324
252627282930  

Posts by Category

Posts by Month


Latest Posts

Interesting Websites:

Most Recent Posts

Syndication

Management

Stats:
eXTReMe Tracker