当前位置: 首页 > 工具软件 > GO daemon > 使用案例 >

go lang 后台(守护进程)运行(daemon)

郎仰岳
2023-12-01

引用:https://bitbucket.org/kardianos/service


service will install / un-install, start / stop, and run a program as a service (daemon).


Currently supports Windows XP+, Linux/Upstart, and OSX/Launchd.


package main
import (
        "bitbucket.org/kardianos/service"
        "fmt"
        "os"
)
var log service.Logger
func main() {
        var name = "GoServiceTest"
        var displayName = "Go Service Test"
        var desc = "This is a test Go service.  It is designed to run well."
        var s, err = service.NewService(name, displayName, desc)
        log = s
        if err != nil {
                fmt.Printf("%s unable to start: %s", displayName, err)
                return
        }
        if len(os.Args) > 1 {
                var err error
                verb := os.Args[1]
                switch verb {
                case "install":
                        err = s.Install()
                        if err != nil {
                                fmt.Printf("Failed to install: %s\n", err)
                                return
                        }
                        fmt.Printf("Service \"%s\" installed.\n", displayName)
                case "remove":
                        err = s.Remove()
                        if err != nil {
                                fmt.Printf("Failed to remove: %s\n", err)
                                return
                        }
                        fmt.Printf("Service \"%s\" removed.\n", displayName)
                case "run":
                        doWork()
                case "start":
                        err = s.Start()
                        if err != nil {
                                fmt.Printf("Failed to start: %s\n", err)
                                return
                        }
                        fmt.Printf("Service \"%s\" started.\n", displayName)
                case "stop":
                        err = s.Stop()
                        if err != nil {
                                fmt.Printf("Failed to stop: %s\n", err)
                                return
                        }
                        fmt.Printf("Service \"%s\" stopped.\n", displayName)
                }
                return
        }
        err = s.Run(func() error {
                // start
                go doWork()
                return nil
        }, func() error {
                // stop
                stopWork()
                return nil
        })
        if err != nil {
                s.Error(err.Error())
        }
}
func doWork() {
        log.Info("I'm Running!")
        select {}
}
func stopWork() {
        log.Info("I'm Stopping!")
}

 类似资料: