我使用 golang 编写了一个简单的 Windows 服务。package mainimport "golang.org/x/sys/windows/svc"import "io/ioutil"import "log"import "encoding/json"import "time"import "strconv"import "os"import "./cmd"import "golang.org/x/sys/windows/svc/eventlog"import "fmt"type myservice struct{}func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { log.Println("Starting service") const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} StartService(r, changes) return}//RunService Runs the current application as a service.It will send the necessary flags to windows.func RunService(maintServiceName string) { //var elog eventlog elog, evtErr := eventlog.Open(maintServiceName) if evtErr != nil { return } defer elog.Close() log.Println("Running service") elog.Info(1, fmt.Sprintf("starting %s service", maintServiceName)) run := svc.Run sererr := run(maintServiceName, &myservice{}) if sererr != nil { elog.Error(1, fmt.Sprintf("%s service failed: %v", maintServiceName, sererr)) }}//StartService Starts the Servicefunc StartService(r <-chan svc.ChangeRequest, changes chan<- svc.Status) { const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue jsonFile, err := os.Open("config.json") if err != nil { log.Fatalln("No configuration found") }该应用程序大部分时间运行一两天都没有问题,但之后停止而没有任何错误。发生这种情况时,日志中的 sleep line 后没有日志。我可以添加任何内容以获取有关服务失败的更多信息。这里可能是什么问题?
- 1 回答
- 0 关注
- 252 浏览
添加回答
举报
0/150
提交
取消