在Windows中,服务无法启动。

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

Service is not starting in Windows

问题

我已经在Go中创建了一个可执行文件,并将其作为Windows服务运行。

我正在使用golang.org/x/sys/windows/svc包,并提供了Execute方法,该方法由SCM调用。

run := svc.Run
_ = run(svcName, &myservice{})

func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {

	changes <- svc.Status{State: svc.StartPending}

 	go mainmethod()

    changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}


}

该服务大部分时间都可以正常工作,但问题是每当我重新启动系统时,服务就会给我一个错误1053:"服务未能及时响应启动或控制请求"的错误,并且无法启动。

在我的run和Execute方法中添加日志语句后,我发现当服务在没有错误的情况下启动时,日志会被打印出来,但是当服务出现1053错误时,SCM甚至不会调用我的可执行文件,因此没有任何日志语句被打印出来。
有人尝试过以类似的方式创建Windows服务吗?这是实现的问题还是Windows svc包的问题?

英文:

I have created an executable in Go and I am running it as a windows service.

I am using golang.org/x/sys/windows/svc package, and provide it with the Execute method which gets called by the SCM

run := svc.Run
_ = run(svcName, &amp;myservice{})

func (m *myservice) Execute(args []string, r &lt;-chan svc.ChangeRequest, changes chan&lt;- svc.Status) (ssec bool, errno uint32) {

	changes &lt;- svc.Status{State: svc.StartPending}

 	go mainmethod()

    changes &lt;- svc.Status{State: svc.Running, Accepts: cmdsAccepted}


}

The service works most of the time but the problem is that whenever I restart my system, the service gives me a Error 1053 : "The service did not respond to start or control request in a timely fashion" error and does not start,

After adding the log statements in my run and Execute methods, I figured out that when the service starts without error the logs get printed, but when the services gives a 1053 error the SCM does not even invoke my exe, since none of the log statements get printed.
Has anyone tried creating a windows service in a similar way? Is it a problem with implementation or the windows svc package?

答案1

得分: 0

我在Program Files中部署了GO exe,我将Windows服务的LogOnAs属性更改为"Local Service",并在"Folder Security"中为"Local Service"提供了对安装文件夹的完全访问权限。

这似乎修复了Windows 10的问题。服务在Windows重新启动时启动,但这个修复方法对于Windows 7和Windows 8仍然不起作用。

编辑:

将服务设置为"Automatic Delayed"可以解决问题,但你需要等待2-3分钟才能在启动后启动服务。

这里提到的答案有所帮助:https://serverfault.com/questions/697608/automatic-windows-service-not-starting/697852#697852?newreg=788f7ab0bb084fec85d1ce2e51bf8317

英文:

I have the GO exe deployed in Program Files, I changed the LogOnAs attribute of Windows Service to "Local Service" and gave "Local Service" complete Access to the installation folder in "Folder Security".

This seems to fix the issue for windows10. The service starts on windows restart, But this fix still does not work for windows 7 and windows8.

Edit:

Making the service "Automatic Delayed" works, however you have to wait for the service start after boot which takes 2-3 minutes.

The answer mentioned here helped: https://serverfault.com/questions/697608/automatic-windows-service-not-starting/697852#697852?newreg=788f7ab0bb084fec85d1ce2e51bf8317

huangapple
  • 本文由 发表于 2017年8月16日 20:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/45713666.html
匿名

发表评论

匿名网友

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

确定