03 November 2011

Run any executable as a service

Generally speaking applications are either foreground user interactive apps or background services.  Sometimes there is a requirement to run one of these foreground applications as a service.  This process will cover this.  I will be using uTorrent in this example.

Additional required software
Service applications are made to be run as services.  Foreground apps are not, to get them to behave like services you need a "service wrapper"  This service will manipulate the application as if it were a service.

There are a few flavors out there but I am going to use instsrv.exe and srvany.exe.  These tools are part of the Windows 2003 resource kit.  http://www.microsoft.com/download/en/details.aspx?id=17657


  • Download and install the resource kit in the default location. c:\Program Files (x86)\Windows Resource Kits\Tools\


(As far as I could figure sc.exe allows you to create the service registry entries but it does not provide a wrapper like srvany does)

Preparing the service account
One of the advantages of running as a service is that you can choose the user account the application runs as.  In the uTorrent example it allows us to specify specific application setting in the application GUI.  When it starts up as that user account those settings are used.  If it is started as another account the default settings are used.

Service accounts are generally never used to log into a machine with.  Instead they tend to be local accounts with non expiring passwords.  This provides a "static" environment for the application to operate in.

  • Create and or Log on as the "service account"
  • Launch the application and make all required changes  - For uTorrent we are enabling the webUI Web interface and setting connection port and credentials.

Creating the service
This is a two step process. The first one will create the registry entries for the service wrapper.  The second part you will need to configure the wrapper settings.

  • Open a command prompt as an administrator
  • Enter the following:

C:\>"c:\Program Files (x86)\Windows Resource Kits\Tools\instsrv.exe" uTorrent " c:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe"


  • It should complete with the line "The service was successfully added!"


At this point you have created the wrapper service but not specified what the wrapper is supposed to execute.


  • Open regedit
  • Expand HKLM\System\CurrentControlSet\Services\
  • Located the service you just created (uTorrent)
  • Create a new sub key called Parameters
  • In the Parameters key create a string called Application
  • Specify the path to your executable no quotes required




At this point you now have a service that will execute your required application.  Next up you must define options you want to apply to the actual service.

Configuring the service
The following can be done from the services manager or by using sc.exe

  • Open the services manager
  • Locate and open the uTorrent service
  • From the General tab choose the Startup type (normally Automatic)
  • On the Log On tab change it to the service account created earlier

If it is a local account you can use .\account otherwise it must be domain\account


For far more granular setting pertaining to the service use the sc.exe command line utility.  Check http://support.microsoft.com/kb/251192 for more info.  This allows you do do things like set a description set the severity of the service failure etc.

Testing
To see that everything is working perfectly you should be able to restart the machine. Log on as another user and check the following:

  • The service should be running
  • The process should be running as your specified users
  • The application should be up and running as expected




More on using torrents
http://fixmyitsystem.com/2011/10/building-corporate-torrent-tracker.html
http://fixmyitsystem.com/2011/10/using-private-torrent-to-transfer-large.html
http://fixmyitsystem.com/2011/11/run-any-executable-as-service.html



Additional Links
http://support.microsoft.com/kb/137890/en-us

http://support.microsoft.com/kb/197178
http://www.deciphered.net/blog/2007/04/25/howto_install_utorrent_service_windows_home_server_whs_ctp
http://www.ehow.com/how_7421438_run-utorrent-service.html


No comments:

Post a Comment