Home > IT Architecture, Linux, Systems > Auto-monitor your server status and restart or reboot server(bash shell script)

Auto-monitor your server status and restart or reboot server(bash shell script)

November 22nd, 2010

I'm now in charge of about 70 vps.Every vps has www web-server & proftpd ftp server & mysql db server on it.Now I want to know every server's status.There's of course method that notify you waking up at mid-night and handling with the simple reboot of apache & mysql & proftpd,yes,that's true,for your boss won't be happy enough to see all sites offline on servers that are within your duty.This is called a 'pull' working style.Ok.Now why don't we try the 'push' working style now?Just write the following script on your server,and add it to the cron schedule at a proper execution rate.

The bash script auto-detect and monitor status of your service on a server,and if it find that the software has stopped working,the script will actively start it.And,if the number of  httpd or mysqld exceeds the point you set in the script,the server will restart.All of this primarily should be your working scope,and now it's all the bash script's duty!

Here goes the script:
#!/bin/bash
#check apache,mysql thread and auto reboot system
#Powered by ipaddr(aspbiz)
#Modified by doxer.org
which os is the system,debian or centos;others exit 1
ip_address='xxx.xxx.xxx.xxx'
mail_doxer()
{
mail -s "Abnormal Message from VPS $ip_address" yourmail@gmail.com</tmp/result.txt
}
echo ''>/tmp/result.txt
os="";
apachename="";
grep "centos" /etc/issue -i -q
if [ $? = '0' ];then
os='centos'
apachename="httpd"
fi

grep "debian" /etc/issue -i -q
if [ $? = '0' ];then
os='debian'
apachename="apache"
fi

if [ $os = "" ];then
echo "not a valid system os"
exit 1
fi

ApacheThread=`ps -A|grep ${apachename}|wc -l`
MysqldThread=`ps -A|grep mysql|wc -l`
ProftpThread=`ps -A|grep proftpd|wc -l`

if [ $ApacheThread -eq 0 ]
then
echo "Apache Server has stoped, it will start at $(date +"%y-%m-%d %H:%M:%S")">>/tmp/result.txt
mail_doxer
/etc/init.d/apache2 start
fi

if [ $MysqldThread -eq 0 ]
then
echo "MySQL Server has stoped, it will start at $(date +"%y-%m-%d %H:%M:%S")">>/tmp/result.txt
mail_doxer
/etc/init.d/mysql start
fi

if [ $ProftpThread -eq 0 ]
then
echo "ProftpThread Server has stoped, it will start at $(date +"%y-%m-%d %H:%M:%S")">>/tmp/result.txt
mail_doxer
/etc/init.d/proftpd start
fi

#process forks too much,needs reboot the system
MaxApacheThread=30
MaxMysqlThread=250

NeedReboot=0

if [ $ApacheThread -gt $MaxApacheThread ]
then
NeedReboot=1
fi

if [ $MysqldThread -gt $MaxMysqlThread ]
then
NeedReboot=1
fi

if [ $NeedReboot -eq 1 ]
then
date_reboot=$(date +"%y-%m-%d %H:%M:%S")

echo "System is busy,reboot\nApache:$ApacheThread;Mysql:$MysqldThread;Vsftp:$ProftpThread.">>/tmp/result.txt
mail_doxer
reboot
fi

Ok.Now add the script to your cron schedule by crontab -e and type in the following(change it to meet your needs):
5/* * * * * /usr/local/apache_mysql_ftp_check_restart.sh #every 5 minutes

Good Luck!


  1. May 6th, 2011 at 22:39 | #1

    Good one and also look into http://mmonit.com/monit/

    • lamp-linux
      May 8th, 2011 at 08:57 | #2

      Seems Nice, and most important is that it’s open source. I’ll try it someday. Thanks.

Comments are closed.