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

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐