How to run an application as a Windows service

Heckert GNU whiteIf you ever wanted to run an application as a Windows service, maybe you tried to simply create the service with the main executable of the application. And then you found out that it won’t work. The reason is that in Windows, a service process has to communicate with the SCM (Windows Service Manager). Otherwise the SCM thinks that the process is stuck and kills it.

The solution is to use a wrapper. An executable that can communicate with the SCM and as a child process runs the original program. There are a lot of wrappers. I was searching for one that is really simple, small and justworks™. And I found SrvStart.

There are two executables inside the ZIP package. srvstart.exe, the wrapper itself, and svc.exe, used to manage Windows services. The usage is rather simple. You can use an ini file or pass all the parameters in the command line directly. I prefer an ini file. Now all the steps:

  1. Place the content of the SrvStart’s ZIP package to a directory on your harddrive, eg. C:\SrvStart.
  2. Create an ini file with your favorite editor, eg. <appname>.ini, with following content: 
    [appname]
    startup="path to the application's exe"
    shutdown_method=winmessage
    


    I like to place it in a subdirectory called ini in SrvStart’s directory. It’s better when you have more apps and ini files. shutdown_method parameter tells SrvStart to send close signal on stop service event to the app instead of just killing it.

  3. Since I am used to Microsoft’s SC utility to manage services, I use it for creating the service itself (note the space after =): 
    SC CREATE appname DisplayName= "App Name" binPath= "C:\SrvStart\srvstart.exe appname -c C:\SrvStart\ini\appname.ini" start= auto
    


    If you want to add a description:

     

    SC DESCRIPTION appname "Application description"
    


That’s it. The service should be now visible in Services list (services.msc). Don’t forget that the service, by default, runs under Local System account (think about file permissions and network access for example). Also any pop-up windows from the application will not be visible.

This is just a minimalist configuration, there are more parameters to use in the ini file, described nicely in the documentation available in the documentation directory (services.htm).

Share
This entry was posted in Windows and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

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>

*
*