Windows Services: Total Control

Taming stubborn services made easy

Batch scripts. Why on Earth do we still care about batch scripts? I could give you a lengthy explanation about why I don’t like Powershell, but it’s a hot day and I want to make it short: because it’s there and it’s okay if it gets your job done. So what’s the deal?

You sometimes have to restart services. Perhaps at night, when you’re asleep. Most of the time a simple sc stop <servicename> will do, but there also are services that start several processes (which wouldn’t probably stop if you just stop their caller), and some services just act awkwardly when you stop them: they stop and stop and stop… and never terminate, so you have to kill them. Bad time if you want to happen this automatically.

Here’s a remedy: control-service takes up to five parameters:

  • -super (interactive mode only): reruns the batch as superuser, taming processes you normally wouldn’t be able to stop with your logon account
  • -restart: restarts the service after stopping it
  • -killall: if -killall is not set, control-service just stops the service. If it “stalls” and keeps in a “stopping” state without terminating, control-service kills the service using taskkill. If -killall is set, the service and every other task of the account specified with -account (see below) is killed. This is useful for processes that are started by the service you want to stop. Use -killall only with -account.
  • -service : specifies the name (not display name) of the service to be stopped (and re-started if -restart is set)
  • -account : kills all processes running as . Use this only with -killall

Any order of command line args is valid. I didn’t invent this all by myself; the parsing of command line parameters was inspired by this discussion, and entering elevated mode was discussed here. I hope you find this useful.

Download this code from GitHub.

About Manfred Berndtgen

Manfred Berndtgen, maintainer of this site, is a part-time researcher with enough spare time for doing useless things and sharing them with the rest of the world. His main photographic subjects are made of plants or stones, and since he's learning Haskell everything seems functional to him.