Golang Windows服务包初始化设置

huangapple go评论81阅读模式
英文:

golang windows service package initialize settings

问题

我正在尝试使用这个包创建一个使用golang的Windows服务:

godoc.org/golang.org/x/sys/windows/svc

一切都很顺利,直到我添加了一个init函数来初始化设置。然后服务就无法启动了。我试图通过这个包加载config.json:

github.com/spf13/viper

然后我得到的错误是:

无法启动服务:服务未能及时响应启动或控制请求。

英文:

I am trying to create a golang windows service utilizing this package

godoc.org/golang.org/x/sys/windows/svc

Everything works great until I add an init func to initialize settings. The service then fails to start. I am attempting to load config.json via this package

github.com/spf13/viper

The error I get then is:

could not start service: the service did not response to the start or control request in a timely fashion.

答案1

得分: 2

@JimB 谢谢。就是这样,

在调试模式下,它会在exe所在的文件夹中查找,
而在服务模式下,它会在system32文件夹中查找。

我安装了github.com/kardianos/osext,
并使用了函数ExecutableFolder()来返回到exe文件的主文件夹。这样它就能找到我的配置文件并加载了。

英文:

@JimB thank you. that was it,

running in debug it looks in the folder that the exe is in,
when running in service it looks in the system32 folder.

I installed github.com/kardianos/osext
and used the func ExecutableFolder() to get back to the main folder of the exe file. Which then allowed it to find my config file and load.

huangapple
  • 本文由 发表于 2016年2月24日 21:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/35604138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定