func Status() { if !util.FileExists(util.PidFile()) { fmt.Println("REX-Ray is stopped") return } pid, _ := util.ReadPidFile() fmt.Printf("REX-Ray is running at pid %d\n", pid) }
func Restart() { checkOpPerms("restarted") if util.FileExists(util.PidFile()) { Stop() } Start() }
func Stop() { checkOpPerms("stopped") if !util.FileExists(util.PidFile()) { fmt.Println("REX-Ray is already stopped") panic(1) } fmt.Print("Shutting down REX-Ray...") pid, pidErr := util.ReadPidFile() failOnError(pidErr) proc, procErr := os.FindProcess(pid) failOnError(procErr) killErr := proc.Signal(syscall.SIGHUP) failOnError(killErr) fmt.Println("SUCCESS!") }
func Start() { checkOpPerms("started") log.WithField("os.Args", os.Args).Debug("invoking service start") pidFile := util.PidFile() if util.FileExists(pidFile) { pid, pidErr := util.ReadPidFile() if pidErr != nil { fmt.Printf("Error reading REX-Ray PID file at %s\n", pidFile) } else { fmt.Printf("REX-Ray already running at PID %d\n", pid) } panic(1) } if fg || client != "" { startDaemon() } else { tryToStartDaemon() } }
func startDaemon() { fmt.Printf("%s\n", RexRayLogoAscii) var success []byte var failure []byte var conn net.Conn if !fg { success = []byte{0} failure = []byte{1} var dialErr error log.Printf("Dialing %s\n", client) conn, dialErr = net.Dial("unix", client) if dialErr != nil { panic(dialErr) } } writePidErr := util.WritePidFile(-1) if writePidErr != nil { if conn != nil { conn.Write(failure) } panic(writePidErr) } defer func() { r := recover() os.Remove(util.PidFile()) if r != nil { panic(r) } }() log.Printf("Created pid file, pid=%d\n", os.Getpid()) init := make(chan error) sigc := make(chan os.Signal, 1) stop := make(chan os.Signal) signal.Notify(sigc, syscall.SIGKILL, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func() { rrdaemon.Start(c.Host, init, stop) }() initErrors := make([]error, 0) for initErr := range init { initErrors = append(initErrors, initErr) log.Println(initErr) } if conn != nil { if len(initErrors) == 0 { conn.Write(success) } else { conn.Write(failure) } conn.Close() } if len(initErrors) > 0 { return } sigv := <-sigc log.Printf("Received shutdown signal %v\n", sigv) stop <- sigv }