How to automatically restart Apache Service on Windows (and recover from crash)

Automatic Service Recovery

Use Windows’ built-in Service Recovery to restart Apache on each crash.

Run services.msc.

Select/open the Apache service.

(WampDeveloper’s Apache service is named “WAMPDev.Apache”)

Under Recovery, select to restart the service on each crash…

First failure: Restart the Service
Second failure: Restart the Service
Subsequent failures: Restart the Service

Reset fail count after: 1 day
Restart service after: 1 minute

(no other options selected)

This will attempt to restart Apache on a crash, after waiting 1 minute.

Limitations

The above Service Manager will only restart a crashed Service if that service crashes with a returned non-zero exit code…

If the service crashes in any other way, such as with a zero value, or such as with no exit code – the Manager will NOT attempt to restart the Service. As that would suggest the crash was completely uncontrolled (i.e., the process blew up / the process did not catch nor handle any of the inner exceptions), and that the issue will continue to persist without further user-intervention.

Note that when Apache exits with error code 255, it is restarting itself automatically due to normal operations (such as when reaching the Recycle workers count), and this does not affect the Windows Services Manager restart counts…

[Sun Oct 30 13:28:15 2011] [notice] Parent: child process exited with status 255 -- Restarting.
[Sun Oct 30 13:28:16 2011] [notice] Apache/2.2.20 (Win32) mod_ssl/2.2.20 OpenSSL/0.9.8r configured -- resuming normal operations

(WampDeveloper’s path to log file is C:\WampDeveloper\Logs\Apache\httpd.host.errorlog.txt)

Windows Tasks

You can setup Windows Tasks to monitor and restart the Apache service via either a Batch file or a PHP script.

A script can also check your websites, and restart the Apache service if no response arrives.

Note that all Tasks need to be elected to run with elevated privileges since a Service is being controlled.

Batch File

The simplest possible Task is to just keep starting Apache every X minutes. If the service is already running, the command will be ignored…

net start WAMPDev.Apache

For more advanced options, check –
StackOverflow’s How to check if a service is running via batch file and start it, if it is not running?
SuperUser’s Automatically restart Windows service if it is not running.

PHP Script

Instead of a bat/batch file, you can also schedule a PHP script to execute via an independent/stand-alone PHP process…

Use run path:

C:\WampDeveloper\Components\Php\php.exe -n -f "C:/path/to/script.php"

The “-n” switch tells PHP not to load php.ini: no extensions, default settings. This would be ideal for simple scripts.

Application Monitoring

If the native Windows Task Recovery does not work, and/or for more advanced options, you would need an application to monitor and restart the service such as ServiceProtector or AlwaysUp.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>