Restart Apache Automatically after Crash Script

Apache on my server was crashing regularly due to lack of memory which is a very common problem on shared environment. Most of the VPS servers have very strict resource allocation policy and wordperss and drupal sites are resource intensive, specially mwolk so as soon as memory was running out apache was crashing and I had to manually restart apache which sucked as mwolk sometimes stayed down for hours (I stays online almost 14 hours a day with mwolk open all the time still the site being down is not acceptable by any means)

So now i have installed a shell script which checks whether apache processs is running in every 5 minutes and if it sees it is dead, it restarts apache. It has worked really well for the site as now I don’t see the site down for more then 5 minutes, of course upgrading to bigger VPS plan also helped alot.

So if you want to restart apache automatically if it crashes, just copy the below code and name is restart_apache.sh and put it in cron which gets executed in 5 minutes.

#!/bin/bash
 
# RHEL / CentOS / Fedora Linux restart command
RESTART="/etc/rc.d/init.d/httpd restart"
 
#path to pgrep command
PGREP="/usr/bin/pgrep"
 
# Httpd daemon name,
# Under RHEL/CentOS/Fedora it is httpd
# Under Debian 4.x it is apache2
HTTPD="httpd"
 
# find httpd pid
$PGREP ${HTTPD}
 
if [ $? -ne 0 ] # if apache not running
then
 # restart apache
 $RESTART
fi

You can put this script anywhere in your home directory, but make sure you implement the cron properly.

Related posts:

  1. Apache Crashing due to Excessive Memory usage - Sites not loading Problem If your on a shared host or on VPS and...
  2. Port 80 Busy | How to start Apache on XAMPP and WAMP While using XAMPP or WAMP, many people get this error...
  3. Get Rid of Computer Restart Caused by Windows Auto Update Windows update keeps us updated with newly available patches, security...
  4. Localhost not working but 127.0.0.1 does This happens many times on windows, both XP and Vista...
  5. Install PHP, MySQL and Apache on Windows If you want to Install PHP, MySQL and Apache on...


Liked this post ? Subscribe to MWolk Blog via RSS Feed or via Email and receive free daily Tech and Money making tips.

No Responses to “Restart Apache Automatically after Crash Script”

No feedback yet.

Leave a Reply

Name Email Website URI