diff options
Diffstat (limited to 'internal/cli/stop/command.go')
-rw-r--r-- | internal/cli/stop/command.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/cli/stop/command.go b/internal/cli/stop/command.go new file mode 100644 index 00000000..8b4b589f --- /dev/null +++ b/internal/cli/stop/command.go @@ -0,0 +1,53 @@ +package stop + +import ( + "log" + "os" + "strconv" + "syscall" + + "github.com/roadrunner-server/errors" + "github.com/spf13/cobra" +) + +const ( + // sync with root.go + pidFileName string = ".pid" +) + +// NewCommand creates `serve` command. +func NewCommand(silent *bool) *cobra.Command { + return &cobra.Command{ + Use: "stop", + Short: "Stop RoadRunner server", + RunE: func(*cobra.Command, []string) error { + const op = errors.Op("rr_stop") + + data, err := os.ReadFile(pidFileName) + if err != nil { + return errors.Errorf("%v, to create a .pid file, you must run RR with the following options: './rr serve -p'", err) + } + + pid, err := strconv.Atoi(string(data)) + if err != nil { + return errors.E(op, err) + } + + process, err := os.FindProcess(pid) + if err != nil { + return errors.E(op, err) + } + + if !*silent { + log.Printf("stopping process with PID: %d", pid) + } + + err = process.Signal(syscall.SIGTERM) + if err != nil { + return errors.E(op, err) + } + + return nil + }, + } +} |