amuck-landowner

Quick and dirty webserver monitoring script.

KuJoe

Well-Known Member
Verified Provider
Here's a simple little script I use on my webservers, very helpful to get your website back online when you're not around.


Code:
http=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost)
if [ $http -ne 200 ] ; then
        echo "nginx is down at `date`" >> /var/log/httpchk.log
        /etc/init.d/nginx restart
else
        echo "nginx is up at `date`" >> /var/log/httpchk.log
fi

Just replace nginx with your webserver of choice. For MySQL powered sites I usually have it restart MySQL also in case that is the cause of the site being down.

 

All you need to do is add this code to a new file (I use /root/httpchk.sh) and then add the following to your crontab file (crontab -e on most servers):


Code:
*/5 * * * * /bin/sh /root/httpchk.sh

I find this comes in handy when you are using a VPS with small amounts of RAM or during various small attacks that fill up your webserver's connections.

 

I had somebody ask about adding code for sending an e-mail, I considered doing this but I have 2 monitoring services (1 custom and NodePing) that alert me of any outage and provide me the error code but I will expand on this code with that shortly.

 

Thanks Jono20201 for filling in some blanks I left out.
 
Last edited by a moderator:

Hassan

New Member
Verified Provider
Here's a simple little script I use on my webservers, very helpful to get your website back online when you're not around.



http=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost)
if [ $http -ne 200 ] ; then
echo "nginx is down at `date`" >> /var/log/httpchk.log
/etc/init.d/nginx restart
else
echo "nginx is up at `date`" >> /var/log/httpchk.log
fi

Just replace nginx with your webserver of choice. For MySQL powered sites I usually have it restart MySQL also in case that is the cause of the site being down. I find this comes in handy when you are using a VPS with small amounts of RAM or during various small attacks that fill up your webserver's connections.
Going to use this on some of my managed clients VPS', thanks KuJoe
 

KuJoe

Well-Known Member
Verified Provider
Paste it in a shell file somewhere on your Linux system (for instance called check.sh in /root/) and then create a cron to check it (crontab -e):


*/5 * * * * /bin/sh /root/check.sh
Thanks, I should have been more clear in my original post but I was a bit tired. I'll append your post to mine for clarity. :)
 
Top
amuck-landowner