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
                                (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        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
                                (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        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
                                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        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:
[xml]
















[/xml]
The sleep time of 5 seconds probably needs to be adjusted for different services.

Conclusion

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.

9 Comments

  1. josh wrote:

    PsTools from SysInternals comes with psservice.exe which has a “restart” command.

    Wednesday, March 28, 2007 at 8:54 pm | Permalink
  2. Pankaj wrote:

    Nice explanation. But, what if the service doesn’t stop and just hang? Is there a way to check that and kill it?

    Thursday, September 13, 2007 at 10:43 am | Permalink
  3. Kevin wrote:

    Not sure if you were aware but if you were doing this from CMD, you could achieve this in one command as below with timeout command, bit long winded I know:

    D:\sc \\ stop & timeout /T 5 && sc \\
    servername> start && timeout /T 5 && sc \\ query

    Monday, April 7, 2008 at 4:44 am | Permalink
  4. Shubhashish wrote:

    Indeed a helpfull note to follow, but the solution you are talking about, I doubt whether it will work in Windows 2000 /VISTA , since both of them lacking the sc.exe

    Monday, June 30, 2008 at 11:56 am | Permalink
  5. Eduard Grebenyukov wrote:

    Hi!

    You can use:
    net stop service & net start service

    The “net stop” commant will awaits when service is stopped.

    It is better than using of “timeout” since “net stop” waits till service stopped, not longer.

    Saturday, October 25, 2008 at 7:00 am | Permalink
  6. Thomas J Mason wrote:

    I’m on Vista, and it DOES have “sc.”

    What ‘Vista’ are you looking at?

    Saturday, April 4, 2009 at 12:54 am | Permalink
  7. Jasminaero wrote:

    Ihr habt eine schoene Webseite hier, und vielciht schaut Ihr euch auchmal meine an, ok Sex im Internet ist nicht jedermans Sache, aber eben meine erste Homepage. Danke und macht weiter so! http://pornobilder.sexblogz.org

    Thursday, October 29, 2009 at 5:42 pm | Permalink
  8. Mukesh Koshy M wrote:

    Good article,

    But do anyone know how to start a service on remote machine using username and password?

    For eg. My server name is 54.50.122.75 and the service can be restarted only using username user1 and password pass1

    Any one please suggest on how to do this?

    Monday, October 31, 2011 at 5:05 am | Permalink
  9. To stop/kill a windows process/service remotely, use pskill

    pskill -t \\servername -u username -p password pid

    http://technet.microsoft.com/en-us/sysinternals/bb896683

    cheers!

    Wednesday, November 2, 2011 at 7:44 am | Permalink