func (p *program) Stop(s service.Service) error { close(p.exit) logger.Info("Stopping ", p.DisplayName) if p.cmd.ProcessState.Exited() == false { p.cmd.Process.Kill() } if service.Interactive() { os.Exit(0) } return nil }
func (p *program) Start(s service.Service) error { if service.Interactive() { logger.Info("Running in terminal.") } else { logger.Info("Running under service manager.") } p.exit = make(chan struct{}) // Start should not block. Do the actual work async. go p.run() return nil }
func (p *program) run() { logger.Info("Starting ", p.DisplayName) defer func() { if service.Interactive() { p.Stop(p.service) } else { p.service.Stop() } }() if p.Stderr != "" { f, err := os.OpenFile(p.Stderr, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) if err != nil { logger.Warningf("Failed to open std err %q: %v", p.Stderr, err) return } defer f.Close() p.cmd.Stderr = f } if p.Stdout != "" { f, err := os.OpenFile(p.Stdout, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777) if err != nil { logger.Warningf("Failed to open std out %q: %v", p.Stdout, err) return } defer f.Close() p.cmd.Stdout = f } err := p.cmd.Run() if err != nil { logger.Warningf("Error running: %v", err) } return }