1. 程式人生 > >go lang 後臺(守護程序)執行(daemon)

go lang 後臺(守護程序)執行(daemon)

引用: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!")
}