dongzi0850 2016-02-02 10:35
浏览 80
已采纳

如何配置用Go编写的Windows服务的失败操作?

I'm writing a Windows Service in Go using the golang.org/x/sys/windows/svc package.

So far, it's all working great and so easy to get started with, I love it.

I've written in some auto update functionality and I'm wanting the service to restart itself when it has completed an update.

I've tried spawning a process that will restart the service using the SCM, but it logs an error message which seems to be to do with trying to control the service while running as Local System.

The service process could not connect to the service controller. 

A better/easier way seems to be to os.Exit(1) and have the service Failure Actions set to Restart on Failure, which works brilliantly!

The only trouble is, there doesn't seem to be the functionality to configure those options programatically using Go.

I've done some digging and it looks like they are configured by passing a struct to ChangeServiceConfig2 in advapi32.dll - How to create service which restarts on crash

In golang/sys/blob/master/windows/svc/mgr/config.go - func updateDescription(handle windows.Handle, desc string) error

The code already calls windows.ChangeServiceConfig2 which is a link to the DLL call.

And the Microsoft docs for the SERVICE_FAILURE_ACTIONS struct are here.

I'm having trouble figuring out how to build and pass that struct using Go - does anyone have any insights?

  • 写回答

1条回答 默认 最新

  • doudoulb1234 2016-02-25 13:27
    关注

    After some guidance from here, plus reading through the source code for the existing Go Windows Service interface, I came up with my own answer, which I'll try to document below.

    For type reference when working with the Windows DLLs, the MSDN docs are here.

    My code looks like this:

    import (
        "unsafe"
        "golang.org/x/sys/windows"
    )
    
    const (
        SC_ACTION_NONE                      = 0
        SC_ACTION_RESTART                   = 1
        SC_ACTION_REBOOT                    = 2
        SC_ACTION_RUN_COMMAND               = 3
    
        SERVICE_CONFIG_FAILURE_ACTIONS      = 2
    )
    
    type SERVICE_FAILURE_ACTIONS struct {
        ResetPeriod     uint32
        RebootMsg       *uint16
        Command         *uint16
        ActionsCount    uint32
        Actions         uintptr
    }
    
    type SC_ACTION struct {
        Type            uint32
        Delay           uint32
    }
    
    func setServiceFailureActions(handle windows.Handle) error {
        t := []SC_ACTION{
            { Type: SC_ACTION_RESTART, Delay: uint32(1000) },
            { Type: SC_ACTION_RESTART, Delay: uint32(10000) },
            { Type: SC_ACTION_RESTART, Delay: uint32(60000) },
        }
        m := SERVICE_FAILURE_ACTIONS{ ResetPeriod: uint32(60), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0])) }
    
        return windows.ChangeServiceConfig2(handle, SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&m)))
    }
    

    In my basic example, you need to pass a Service Handle, then it'll set the failure actions to a hard coded default of:

    1. Restart the first time after 1 second.
    2. Restart the second time after 10 seconds.
    3. Restart the third time and any subsequent times after 60 seconds.
    4. Reset the failure counter after 60 seconds.

    I've just tested and it seems to work ok.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。