Skip to content

Restart a windows service remotely

I’ve been working with Shibboleth, an Internet2 project to ease authentication and authorization across institutions. So far, its a mess of XML files, and its been a lot of guess and check on various magic strings. One annoyance in working with it was that I’d update the config files, and then need to remote into the server to restart the windows service using those files. Shibboleth will reread some of the config, but not all, so to be safe I just restart it.

After making a nant task to publish the latest config files for me, the need to click around to restart got very annoying. I wanted to get a way to restart it from the command line so I could restart it from my nant task. After a little googling and some futzing, I got it.

The solution

The program to use is sc.exe, a Service Controller. Unlike net.exe, sc lets you specify a server, so you can do things remotely.

Find the service name

You’ll need the exact service name, which is stored in the registry. An easy way to find it is to enumerate all the services on the box, and then usually you can pick it out from there.

$ sc.exe \\\\server query | less

That will be a long list, so pipe it to less and page through it. That will list an entry for each service, like so:

SERVICE_NAME: shibd_Default
DISPLAY_NAME: Shibboleth 1.3 Daemon (Default)
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

This lets me know the service name I want to be using is “shibd_Default”.

Stopping the service

Stopping is another simple command:

$ sc.exe \\\\server stop shibd_Default

SERVICE_NAME: shibd_Default
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x2
        WAIT_HINT          : 0x0

Note that is doesn’t wait to stop the service, it merely makes a request to stop, then prints out the current status. The status is predicably STOP_PENDING. It’s important to note that sc.exe doesn’t wait for the service to stop, and that the request to stop may fail.

Starting the service

Starting is another easy command:

$ sc.exe \\\\server start shibd_Default

SERVICE_NAME: shibd_Default
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x1
        WAIT_HINT          : 0xbb8
        PID                : 2628
        FLAGS              :

This also only puts in the request for the service to start. As with stop, this request may fail.

Restarting using nant

To put it all together in nant, here’s the plan:

  1. Send the stop request
  2. Sleep a little bit, to give the service a chance to stop
  3. Send the start request
  4. Sleep a little bit, to give the service a chance to start
  5. Send a query request, to check that it started ok

Here’s a simplistic snippet of Nant xml for this:

The sleep time of 5 seconds probably needs to be adjusted for different services.


For once I’m not horribly disappointed in the tools microsoft has provided me. It would be nice if there was a “restart” command, or if you could tell sc.exe to wait until the service actually started or stopped, but there’s at least something workable. Possible improvements would be to make this poll using sc \\server query shibd_Default to check for when the service actually stops or starts, I imagine 5 seconds isn’t a universal upper bound.

If I get ambitious I might package up a nant task wrapping sc and send it to nantcontrib.