Bash script to check and restart apache, mysql and sendmail if it’s down
Bash script to check and restart apache, mysql and sendmail if it’s down
#!/bin/sh
################ Check MySQL #################
STARTAPACHE="/etc/init.d/httpd start"
STARTMYSQL="/etc/init.d/mysqld start"
STARTMAIL="/etc/init.d/sendmail start"
LOGFILE=/home/jackassmccoy/servicelogs/logfile
SERVICE='mysqld'
echo "" >> $LOGFILE
echo `date` >> $LOGFILE
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is OK" >> $LOGFILE
else
echo "$SERVICE is not running, restarting $SERVICE" >> $LOGFILE
checkmysql=`ps ax | grep -v grep | grep -c mysqld`
if [ $checkmysql -le 0 ]
then
$STARTMYSQL
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service is now restarted, everything is OK" >> $LOGFILE
fi
fi
fi
############### Check Apache ################
SERVICE='httpd'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is OK" >> $LOGFILE
else
echo "$SERVICE is not running, restarting $SERVICE" >> $LOGFILE
checkapache=`ps ax | grep -v grep | grep -c httpd`
if [ $checkapache -le 0 ]
then
$STARTAPACHE
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service is now restarted, everything is OK" >> $LOGFILE
fi
fi
fi
############### Check Sendmail ####################
SERVICE='sendmail'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is OK" >> $LOGFILE
else
echo "$SERVICE is not running, restarting $SERVICE" >> $LOGFILE
checksendmail=`ps ax | grep -v grep | grep -c sendmail`
if [ $checksendmail -le 0 ]
then
$STARTMAIL
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service is now restarted, everything is OK" >> $LOGFILE
fi
fi
fi
exit 0