title: How do I configure failure actions of a Windows service written in Go?
body: <p>I'm writing a Windows Service in Go using the <code>golang.org/x/sys/windows/svc</code> package.</p>
  
  <p>So far, it's all working great and so easy to get started with, I love it.</p>
  
  <p>I've written in some auto update functionality and I'm wanting the service to restart itself when it has completed an update.</p>
  
  <p>I've tried spawning a process that will restart the service using the <code>SCM</code>, but it logs an error message which seems to be to do with trying to control the service while running as Local System.</p>
  
  <pre><code>The service process could not connect to the service controller. 
  </code></pre>
  
  <p>A better/easier way seems to be to <code>os.Exit(1)</code> and have the service <code>Failure Actions</code> set to <code>Restart on Failure</code>, which works brilliantly!</p>
  
  <p>The only trouble is, there doesn't seem to be the functionality to configure those options programatically using Go.</p>
  
  <p>I've done some digging and it looks like they are configured by passing a struct to <code>ChangeServiceConfig2</code> in <code>advapi32.dll</code> - <a href="https://stackoverflow.com/questions/3242505/how-to-create-service-which-restarts-on-crash">How to create service which restarts on crash</a></p>
  
  <p>In <em>golang/sys/blob/master/windows/svc/mgr/config.go</em> - <code>func updateDescription(handle windows.Handle, desc string) error</code></p>
  
  <p>The code already calls <code>windows.ChangeServiceConfig2</code> which is a link to the DLL call.</p>
  
  <p>And the Microsoft docs for the <code>SERVICE_FAILURE_ACTIONS</code> struct are <a href="https://msdn.microsoft.com/en-us/library/ms685939(v=vs.85).aspx" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I'm having trouble figuring out how to build and pass that struct using Go - does anyone have any insights?</p>