Managing Windows Services
By Brandon DuRette
Recently, I found I did not know enough about controlling Windows services from the command line. The Windows Services GUI is fine for some things, but it does not support useful operations, like deleting a service. After some investigation, I found just what I needed, the sc command. According to its help documentation, "SC is a command line program used for communicating with the NT Service Controller and services." Perfect!
When running our integration test suite, by default we install clean Code Collaborator servers for each test, running on a random available port. This prevents the activity in previous tests from corrupting the behavior of the current test (for example, by modifying a user preference). When the tests fail, they intentionally do not clean up after themselves. It means that one of the developers can go poke around on the server to diagnose the source of the problem.
> net start myservice
> net stop myservice
The SC command has a query subcommand, which you would think would do the trick. However, it does not seem to have a way to specify anything about the name of the service, just a few system-level things such as its state and its group. Instead, I built up a command pipeline that will do the same trick (using Cygwin tools):
sc query state= all | grep testCollab | grep SER | cut -f 2 -d " "
The first command queries for all services. The first grep pulls out only the services with our testCollab prefix. The second grep pulls out only the SERVICE_NAME lines (not the DISPLAY_NAME lines). If the service name and display name were different, this would not be required. The cut pulls out the actual service name.Stopping Code Collaborator Test Services
Since running services cannot be deleted directly, we first need to stop all the services. While we could use net stop for this purpose, the SC command has another subcommand stop that stops services. We just need to tack that onto our pipeline to stop the services. Due to some issue in the Windows command processor that I didn't bother to track down, the straightforward approach does not seem to work:
... | xargs sc stop
Results in a string of:The specified service does not exist as an installed service.
Removing Code Collaborator Test Services
After waiting for the services to stop completely, which can take some time depending on how many are running, we just need to delete the services. Unsurprisingly, the final SC subcommand we need is called delete. We use the same command pipeline as we did when stopping the service and simply replace the command.
sc query state= all | grep testCollab | grep SER | cut -f 2 -d " " \
| xargs -n 1 cmd /C sc delete
Thats it. All that is left to do is delete the installs, which in our case is done with ant clean. The thought just occurred to me that this functionality might be useful wrapped up in an Ant task. That's a project for another day.


That was a good post. Thanks for sharing your ideas.
Posted by: Dentist Downey | September 24, 2009 at 01:47 AM