Ubuntu auto restart cron for Apache and Mysql after crash

Websites never sleep but you may want to, this script lets you do that without having to worry about Apache or Mysql crashing randomly.

Obviously if you have reoccurring issues its best to check through the log files and fix any issues but this will help reduce down time on vital software crashes to less than a minute by checking for  Apache and Mysql services running and if none are found they get restarted.

Save this into a .sh file somewhere on your server and run this every minute usinng the built in Ubuntu Cron. If you are using a different OS and this doesn’t work replace the restart commands.

 

#!/bin/bash

# APACHE SECTION
RESTART="/etc/init.d/apache2 restart"
PGREP="/usr/bin/pgrep"
HTTPD="apache"
$PGREP ${HTTPD}
if [ $? -ne 0 ]
then
$RESTART
fi

# MYSQL SECTION
RESTARTM="/etc/init.d/mysql restart"
MYSQLD="mysqld"
$PGREP ${MYSQLD}
if [ $? -ne 0 ]
then
$RESTARTM
fi

 

To improve this script you could add email notifications and other vital services like sendmail which if they go down may be less apparent.

 

Free tech ebooks, the definitive free guides

Learning new programming languages is fun but it can be difficult choosing which references and books to use. Most good text book will cost a small fortune and go out of date within a few months and probably wont cover everything you need.

For me the best resource comes straight from the source, for most languages you will be able to find a FREE and complete reference to develop with.

PHP Documentation – Download the complete reference in most spoken languages.

MSDN Javascript – Microsoft have a vast documentation library available on-line.

Mozilla Javascript Resource – Not great but it goes into a lot of detail, maybe too much.

Core Javascript 1.5 from Mozilla – Learn by example, much better than the previous Mozilla link

w3 CSS v1 – Full guide to CSS v1 from 1996 – 2009 in PDF format.

w3 guide to css 2.1 – Full on-line reference to css v2.1

w3 HTML 3 guide -  An awesome guide to HTML, very handy for SEO Tricks,

Action Script 3 – Official full guide to Action script 3 programming.

CS3 – The full CS3 shabang from Adobe, I haven’t found a decent Flash book yet so this is awesome.

Mysql - One of the less helpful guides but it is fairly complete.

WordPress Functions - The official function list, awesome for any wordpress project