Wednesday, August 14, 2013

Run Elasticsearch as a Service on Windows 2003/2008 Server

UPDATE: As of Elasticsearch release 0.90.5, there is a default Windows service included in elastic search. So, instead of following this guide... upgrade!!!


If you'd like to run Elasticsearch (or any batch or executable file for that matter) as a service on Windows 2003 Server, it's pretty simple and straight-forward. I didn't have a Windows 2008 Server license available to tinker with, but it seems like it's even easier.

Windows 2003 Server

The steps outlined in this section involve editing the registry on your server. If you are not comfortable editing the registry then look elsewhere. See this article for information on backing up and restoring the registry.
  1. Download and extract Elasticsearch to C:\elasticsearch. Make sure you have a Java Runtime Environment (JRE) installed on your system and that you have the JAVA_HOME environment variable configured correctly on your machine and that you can actually get Elasticsearch running before continuing to the next step.
  2. We need srvany.exe on the machine. So, to get that, install Windows Resource Kits if it's not already installed (default installation is C:\Program Files\Windows Resource Kits)
  3. From a command prompt do the following:
    cd "C:\Program Files\Windows Resource Kit\Tools"
    instsrv.exe elasticsearch "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"
    regedt32
  4. Now you should have the registry editor open. In the registry editor go to: HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > Services
  5. Find elasticsearch in the list and right-click on it. Choose New > Key and name the key Parameters
  6. Right-click on the Parameters key and choose New > String Value. Change the name of this value to Application.
  7. Right-click on the Application value and choose Modify, then in the Value data field enter C:\elasticsearch\bin\elasticsearch.bat
  8. Open the Services control panel (Start > Control Panel > Services) and find the elasticsearch service. You will notice that the service is by default set to start automatically when the system starts. Start the service.
*An important note about the configuration described above is that it is good for starting Elasticsearch (or whatever you tell it to), but not good for stopping it. You see, when the service is started, it actually starts srvany.exe with a parameter telling srvany the name of an application to start. In this demo srvany runs elasticsearch.bat which ultimately fires up an instance of java.exe. So, the resulting process that is started is a java.exe process. If you stop the elasticsearch service from the service manager, what it actually stops is the srvany.exe process that was started. If you want to stop Elasticsearch you need to manually stop the java.exe process from the task manager. On the plus-side, if you want to start up additional elasticsearch nodes, you can simply restart the elasticsearch service from the service manager and srvany will launch elasticsearch.bat each time.


Windows 2008 Server

As I mentioned above, I didn't have a Windows 2008 Server license available to tinker with, but from what I can tell you can use the sc create command to achieve the same results. I would be interested to know if this works, so feel free to leave a comment and let me know what you find.
  1. Follow step 1 above
  2. From the command line type the following (note that the command should be all on one line):
    sc create elasticsearch binpath=C:\elasticsearch\bin\elasticsearch.bat start=auto
  3. Follow step 8 above