func TestInstallRunRestartStopRemove(t *testing.T) { p := &program{} s, err := service.New(p, sc) if err != nil { t.Fatal(err) } _ = s.Uninstall() err = s.Install() if err != nil { t.Fatal("install", err) } defer s.Uninstall() err = s.Start() if err != nil { t.Fatal("start", err) } err = s.Restart() if err != nil { t.Fatal("restart", err) } err = s.Stop() if err != nil { t.Fatal("stop", err) } err = s.Uninstall() if err != nil { t.Fatal("stop", err) } }
func main() { svcFlag := flag.String("service", "", "Control the system service.") flag.Parse() configPath, err := getConfigPath() if err != nil { log.Fatal(err) } config, err := getConfig(configPath) if err != nil { log.Fatal(err) } svcConfig := &service.Config{ Name: config.Name, DisplayName: config.DisplayName, Description: config.Description, } prg := &program{ exit: make(chan struct{}), Config: config, } s, err := service.New(prg, svcConfig) if err != nil { log.Fatal(err) } prg.service = s errs := make(chan error, 5) logger, err = s.Logger(errs) if err != nil { log.Fatal(err) } go func() { for { err := <-errs if err != nil { log.Print(err) } } }() if len(*svcFlag) != 0 { err := service.Control(s, *svcFlag) if err != nil { log.Printf("Valid actions: %q\n", service.ControlAction) log.Fatal(err) } return } err = s.Run() if err != nil { logger.Error(err) } }
func runService() { p := &program{} s, err := service.New(p, sc) if err != nil { log.Fatal(err) } err = s.Run() if err != nil { log.Fatal(err) } }
// Service setup. // Define service config. // Create the service. // Setup the logger. // Handle service controls (optional). // Run the service. func main() { svcFlag := flag.String("service", "", "Control the system service.") flag.Parse() svcConfig := &service.Config{ Name: "GoServiceExampleLogging", DisplayName: "Go Service Example for Logging", Description: "This is an example Go service that outputs log messages.", } prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { log.Fatal(err) } errs := make(chan error, 5) logger, err = s.Logger(errs) if err != nil { log.Fatal(err) } go func() { for { err := <-errs if err != nil { log.Print(err) } } }() if len(*svcFlag) != 0 { err := service.Control(s, *svcFlag) if err != nil { log.Printf("Valid actions: %q\n", service.ControlAction) log.Fatal(err) } return } err = s.Run() if err != nil { logger.Error(err) } }
func main() { svcConfig := &service.Config{ Name: "GoServiceExampleSimple", DisplayName: "Go Service Example", Description: "This is an example Go service.", } prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { log.Fatal(err) } logger, err = s.Logger(nil) if err != nil { log.Fatal(err) } err = s.Run() if err != nil { logger.Error(err) } }
func main() { flag.Parse() //setup logging to a file l, err := NewLogger(os.Stderr) if err != nil { log.Fatalf("Failed to create logger: %s", err) } log.SetOutput(l) defer l.Close() //initialize service conf := &service.Config{ Name: "timeglass", DisplayName: "Timeglass", Description: "Automated time tracking daemon that monitors file changes", Option: map[string]interface{}{}, } if runtime.GOOS == "darwin" { conf.Name = "com.timeglass.glass-daemon" // @todo technically this is possible but // results in a automated installer that // has some serious usability issues // conf.Option["UserService"] = true } else if runtime.GOOS == "windows" { //WATCH OUT: timeglass has a windows installer //that takes care of installing and starting services //in addition to the command line conf.Name = "Timeglass" //windows style } d := &daemon{} s, err := service.New(d, conf) if err != nil { log.Fatal(err) } //handle service controls if len(flag.Args()) > 0 { err = service.Control(s, flag.Args()[0]) if err != nil { ReportServiceControlErrors(err) } return } //start daemon log.Printf("Daemon launched, writing logs to '%s'", l.Path()) defer func() { log.Printf("Daemon terminated\n\n") }() err = s.Run() if err != nil { log.Fatal(err) } }