github.com\kardianos\service
参考:https://my.oschina.net/idufei/blog/710885
```
package main
import (
"log"
"github.com/kardianos/service"
"os"
)
var logging service.Logger
type program struct{}
func (p *program) Start(s service.Service) error {
logging.Info("开始服务")
go p.run()
return nil
}
func (p *program) Stop(s service.Service) error {
logging.Info("停止服务")
return nil
}
func (p *program) run() {
// 这里放置程序要执行的代码……
logging.Info("run my ccc")
}
func main(){
//服务的配置信息
cfg := &service.Config{
Name: "simple_test",
DisplayName: "a simple_test service",
Description: "This is an simple_test Go service.",
}
// Interface 接口
prg := &program{}
// 构建服务对象
s, err := service.New(prg, cfg)
if err != nil {
log.Fatal(err)
}
// logger 用于记录系统日志
errs := make(chan error, 5)
logging, err = s.Logger(errs)
if err != nil {
log.Fatal(err)
}
go func() {
for {
err := <-errs
if err != nil {
log.Print(err)
}
}
}()
if len(os.Args) == 2 { //如果有命令则执行
err = service.Control(s, os.Args[1])
if err != nil {
log.Fatal(err)
}
} else { //否则说明是方法启动了
err = s.Run()
if err != nil {
logging.Error(err)
}
}
if err != nil {
logging.Error(err)
}
}
```
`go build -o simple_test main.go`
`./simple_test start`
注册完毕
`service simple_test status`
![image.png](https://static.studygolang.com/190515/63b11125e804e0c68be0e3fd6c39e2c4.png)
**问题**
1、操作 service simple_test uninstall **报错** `simple_test: unrecognized service`
2、找到内部出错问题 `systemctl disable simple_test` 运行出错, :`Failed to execute operation: Connection timed out
`
**求解**
给个建议就可以
#1