daemon icon indicating copy to clipboard operation
daemon copied to clipboard

daemon service does not work in windows10

Open arthur-kb opened this issue 4 years ago • 7 comments

Attempting to start windows service results in a long pause and the following error returned:

Error: The service did not respond to the start or control request in a timely fashion.

arthur-kb avatar Jul 28 '20 05:07 arthur-kb

See my pull request #97 Restructuring your service as i've done with the myservice example should make it work on windows.

filippo-veneri avatar Aug 21 '20 07:08 filippo-veneri

I updated to golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f, but it still does't work.

daniel-007 avatar Sep 07 '20 02:09 daniel-007

Updating the dependency was just a side-effect, not at all necessary. The important thing is to restructure your code as i have done in my pull request. If you clink on the PR link (#97) and then on "Files changed" you'll see what modifications i have done to examples/myservice.go to make it work.

filippo-veneri avatar Sep 07 '20 08:09 filippo-veneri

@filippo-veneri I just looked at the code on your PR and I couldn't help but wonder why and how it "works"? Unless I missed it, I don't see any place where you are calling (daemon.Daemon).Start(). You seem to have implemented your own start method which does something else. At what point does it actually start the daemon (windows service). Could this explain why you are not getting this issue? Appreciate your contribution on this :-)

sithembiso avatar Apr 01 '21 09:04 sithembiso

This is also related to #68

sithembiso avatar Apr 01 '21 10:04 sithembiso

It is a bit convoluted. On Windows the (daemon.Daemon).Install() interface method refers to the (daemon.windowsRecord).Install() concrete method. Assuming your service executable is named myservice.exe, this method creates the windows service using the path of myservice.exe as the service executable. When the windows service is started, myservice.exe is executed without parameters, resulting in service.Run(mysvc) (defined in myservice.go) being called, which, in this case, corresponds to (daemon.windowsRecord).Run(e Executable). This method then calls svc.Run(windows.name, &serviceHandler{executable: e}) which in turn calls (daemon.serviceHandler).Execute(...), which is the main loop of the windows service. In response to the windows service state changes this loop calls the MyService.Start() and MyService.Stop() methods. Note that MyService implements the daemon.Executable interface.

filippo-veneri avatar Apr 01 '21 16:04 filippo-veneri

Note that MyService implements the daemon.Executable interface.

I was running into a similar issue and found @filippo-veneri is correct. The example service does not work on the master branch. Your code needs to implement the daemon.Executable interface and be passed into daemon.Run(MyService) instead of the structure on the master branch. Look at their PR for a proper example https://github.com/takama/daemon/blob/c329ab930e326908bf8d91a622481e6d124343e4/examples/myservice.go#L131

kutterback avatar Jul 27 '23 23:07 kutterback