-
Notifications
You must be signed in to change notification settings - Fork 506
/
stop.go
40 lines (34 loc) · 828 Bytes
/
stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/urfave/cli/v2"
)
// The function defines a CLI command to stop a server by reading a lock file, killing the process with
// the corresponding PID, and removing the lock file.
func StopCmd() *cli.Command {
return &cli.Command{
Name: "stop",
Usage: "stop server",
Action: func(c *cli.Context) error {
appName := c.App.Name
lockFile := fmt.Sprintf("%s.lock", appName)
pid, err := os.ReadFile(lockFile)
if err != nil {
return err
}
command := exec.Command("kill", string(pid))
err = command.Start()
if err != nil {
return err
}
err = os.Remove(lockFile)
if err != nil {
return fmt.Errorf("can't remove %s.lock. %s", appName, err.Error())
}
fmt.Printf("service %s stopped \n", appName)
return nil
},
}
}