Home > Articles

Logs and Monitoring

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Restarting Apache Automatically If It Fails


   if [ `ps -waux | grep -v grep | grep -c httpd` -lt 1

   ]; then apachectl restart; fi

If you install Apache on Windows as a service, it can be automatically restarted by the service manager if it crashes.

In Unix, you can implement this functionality with a watchdog script. A watchdog monitors the status of another program, and if the program crashes or stops for any reason, the watchdog starts it again. The example shows a simple Linux script that will monitor the system process list to ensure that an httpd process exists, and will restart httpd if it crashes. To use it, you will need to give it executable permissions and add it to your cron configuration so it can be run at predefined intervals.

If you are running Solaris, use ps -ef instead of ps - waux.

You can find a more sophisticated watchdog script that will send email when the server is down, and can watch specific httpd process ids, at the following URL: http://perl.apache.org/docs/general/control/control.html.

Most Linux distributions also include their own generic watchdog scripts that can be adapted to work with Apache.

  • + Share This
  • 🔖 Save To Your Account