Run a Windows Application as a Service with srvany

Occasionally the need arises for running an application in Windows as a service. This allows the application to run at boot time without the need for a user to be logged into the system in order for the application to start and operate. While built-in mechanisms such as Task Scheduler exist to help facilitate this, the ability to run an application as a service has certain advantages, notably the ability to prescribe certain actions be taken should the service fail. Take for example the the case where an application is listening for incoming IP connections. Being able to automatically restart the service without user intervention is desirable in order to avoid loss of service.

This post will describe how to use the Microsoft utilities instrsrv.exe and srvany.exe to install a Windows application as a service. instrsrv.exe is used to install the service while srvany.exe acts as a wrapper around the application and handles the service events. Both utilities are available as part of Microsoft’s Windows Server 2003 Resource Kit Tools. I have successful tested instrsrv.exe and srvany.exe on Windows 7/8.1; I have not tested them on Windows 10.

Let’s get started…

For purposes of example, we’ll assume we have a Windows application called “foobar” that is normally started by using the binary c:\foo\foobar.exe.

Start by downloading Microsoft’s Windows Server 2003 Resource Kit Tools and install it to a folder of your choice. Create a folder to contain the files instrsrv.exe and srvany.exe. We’ll use c:\srvany for our example. Now copy both files from the where you installed the Resource Kit tools to c:\srvany. After the files have been copied the Windows Server 2003 Resource Kit Tools may be uninstalled.

Now open a command prompt and install foobar as a system service using the following command. You may use any name you’d like for the service:

You should receive a response indicating that the service was successfully added.

Next, open the Windows registry editor and navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\foobar. Use the mouse or keyboard to highlight foobar and select Edit->New->Key to create a new key named Parameters. Highlight Paramters and select Edit->New->String Value to create a new string value named Application. Double-click on Application and enter the full path to foobar.exe (e.g., c:\foo\foobar.exe). That’s it. You can now close the registry editor.

By default, the newly created service foobar is configured to run automatically when the system is restarted. To change this setting to Manual, run the Services applet by entering services.msc from a command prompt or by using WIN+R. Locate foobar from among the list of services, right-click on it and select Properties and change its Startup type to Manual. A service set to Manual can be started from within the Services applet, or by entering the following from a command prompt:

Selecting the Recovery tab from within properties will allow you to chose what the system should do in the event if the service fails.

You can delete the service by entering the following from a command prompt:

There you have it. A nice simple way to register and run a non-service application as a windows service.

Comments

  1. Luis, thanks for bringing this to my attention. Admittedly I did not test this on Windows 10. However, a quick Google search suggests there may be several ways to create services from binary executables on Windows 10.

  2. my cpu is 7700k, mother board z170 Gaming 7…. RAM 16Gb.
    my cpu usage showing 55-65% all the time, even at idle time…. from task manager i find out srvany is using almost 50%+ of my cpu and causing my cooler fan running on high speed all the time…. is there any way i can stop it or i dont need to worry about it…..??…….>>>

Leave a Reply

Your email address will not be published. Required fields are marked *

iceflatline