关于编写windows service,开始搜索到资料http://sanatgersappa.blogspot.com/2013/07/windows-service-with-go-easy-way.html (需要FQ),感觉NSSM(http://nssm.cc/)挺方便的,考虑使用NSSM程序,使用NSSM把exe程序注册成windows服务,命令:
nssm install MyService d:\MyService.exe
(where d:\MyService.exe is the full path to the exe file).
我当时就写了2个批处理程序CreateService.bat:
nssm install SyncSystemTimeService %CD%\SyncSystemTim.exe
DeleteService.bat:
sc delete SyncSystemTimeService
windows服务注册成功了,启动SyncSystemTimeService服务后,我的程序好像并没有生效,我怀疑要么必须使用完整的路径(不能用%CD%变量), 要么可能使用%~dp0而不是%CD%(参考资料:http://stackoverflow.com/questions/16255184/how-do-i-find-the-current-directory-of-a-batch-file-and-then-use-it-for-the-pat),后来我没有继续查原因,在网上找到了使用golang写的一个服务程序:http://bitbucket.org/kardianos/service, 把代码拉下来,调整一下就达到我想要的要求。
附上完整代码:
BTW: 由于log打印的日志可以在windows的事件查看器中看到,所以我把WriteLogFile函数注释掉了。
小程序,小功能,请轻拍,欢迎批评意见。