<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ryan's Tech Blog &#187; nant</title>
	<atom:link href="http://ryepup.unwashedmeme.com/blog/category/nant/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryepup.unwashedmeme.com/blog</link>
	<description>mostly tech, mostly rants</description>
	<lastBuildDate>Wed, 04 Jan 2012 03:42:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Restart a windows service remotely</title>
		<link>http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/</link>
		<comments>http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/#comments</comments>
		<pubDate>Thu, 04 Jan 2007 19:54:46 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[code snippet]]></category>
		<category><![CDATA[nant]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>
I've been working with <a href="http://shibboleth.internet2.edu/">Shibboleth</a>, 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.
</p>
<p>
After making a <a href="http://nant.sourceforge.net/">nant</a> 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.
</p>
<h3>The solution</h3>
<p>
The program to use is <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sc.mspx?mfr=true">sc.exe</a>, a Service Controller.  Unlike <a href="http://www.windowsitpro.com/Article/ArticleID/14478/14478.html">net.exe</a>, sc lets you specify a server, so you can do things remotely.
</p>
<h3>Find the service name</h3>
<p>
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.</p>
<pre>
$ sc.exe <em>\\\\server</em> query | less
</pre>
<p>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:</p>
<pre>
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
</pre>
<p>This lets me know the service name I want to be using is "shibd_Default".
</p>
<h3>Stopping the service</h3>
<p>
Stopping is another simple command:</p>
<pre>
$ sc.exe <em>\\\\server</em> 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
$
</pre>
<p>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.
</p>
<h3>Starting the service</h3>
<p>
Starting is another easy command:</p>
<pre>
$ sc.exe <em>\\\\server</em> 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              :
</pre>
<p>This also only puts in the request for the service to start.  As with stop, this request may fail.
</p>
<h3>Restarting using nant</h3>
<p>
To put it all together in nant, here's the plan:</p>
<ol>
<li>Send the stop request</li>
<li>Sleep a little bit, to give the service a chance to stop</li>
<li>Send the start request</li>
<li>Sleep a little bit, to give the service a chance to start</li>
<li>Send a query request, to check that it started ok</li>
</ol>
<p>Here's a simplistic snippet of Nant xml for this:</p>
<div class="igBar"><span id="lxml-2"><a href="#" onclick="javascript:showPlainTxt('xml-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-2">
<div class="xml" style="font-family: monospace;">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Send the stop request --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;exec</span> <span style="color: #000066;">program</span>=<span style="color: #ff0000;">&quot;sc.exe&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;\\server stop shibd_Default&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/exec<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Sleep a little bit, to give the service a chance to stop --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sleep</span> <span style="color: #000066;">seconds</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Send the start request --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;exec</span> <span style="color: #000066;">program</span>=<span style="color: #ff0000;">&quot;sc.exe&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;\\server start shibd_Default&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/exec<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Sleep a little bit, to give the service a chance to start --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sleep</span> <span style="color: #000066;">seconds</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Send a query request, to check that it started ok --&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;exec</span> <span style="color: #000066;">program</span>=<span style="color: #ff0000;">&quot;sc.exe&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;\\server query shibd_Default&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/exec<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
The sleep time of 5 seconds probably needs to be adjusted for different services.
</p>
<h3>Conclusion</h3>
<p>
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 <code>sc \\server query shibd_Default</code> to check for when the service actually stops or starts, I imagine 5 seconds isn't a universal upper bound.
</p>
<p>If I get ambitious I might package up a nant task wrapping sc and send it to <a href="http://nantcontrib.sourceforge.net/">nantcontrib</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.693 seconds -->

