Gotchas with Windows Services

In trying to deploy a Windows Service using MSBuild, I learned a few neat things about them.

You can stop, start, create, or delete a Windows Service through the administrator prompt command line:


sc create "AWindowsService" binpath= "C:\Weee\Service.exe"
sc delete "AWindowsService
sc start "AWindowsService"
sc stop "AWindowsService"

You can also do this for another server in your domain, by providing its UNC path:


sc \\ServerName create "AWindowsService" binpath= "C:\Weee\Service.exe"
sc \\ServerName delete "AWindowsService
sc \\ServerName start "AWindowsService"
sc \\ServerName stop "AWindowsService"

When creating a Windows Service, you can decide how it should start up.

Common options:

sc config AWindowsService start= delayed-auto
sc config AWindowsService start= auto
sc config AWindowsService start= demand

Much like other command prompt commands, the name of the service needs to be in quotes if it has spaces in it:


sc start "A Windows Service"
sc start AWindowsService

The spaces for the options are required:


sc \\ServerName create "AWindowsService" binpath= "C:\Weee\Service.exe"
^ Required
sc config AWindowsService start= demand
^ Also Required

If you’re trying to delete a service, you can’t have services.msc running, nor can the service be open, and you can’t have Process Explorer running either (h/t to “StingyJack” in this Stack Overflow answer).

If you create a service with the wrong bin path, it won’t be able to start; you’ll have to modify the bin path or delete and re-create the service.

Other Resources:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s