Here's a simple little script I use on my webservers, very helpful to get your website back online when you're not around.
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):
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.
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: