-
Notifications
You must be signed in to change notification settings - Fork 2
/
stop.go
57 lines (50 loc) · 1.07 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package command
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"syscall"
"github.com/lodastack/agent/config"
"github.com/oiooj/cli"
)
var CmdStop = cli.Command{
Name: "stop",
Usage: "关闭客户端",
Description: "关闭Agent客户端",
Action: runStop,
}
func runStop(c *cli.Context) {
stopAgent()
}
func stopAgent() {
if runtime.GOOS != "linux" {
fmt.Printf("Agent don't support this arch: %s\n", runtime.GOOS)
os.Exit(1)
}
data, err := ioutil.ReadFile(config.PID)
if err != nil {
fmt.Printf("cannot read pid file: %s ", config.PID)
return
}
pid, _ := strconv.Atoi(string(data))
process, err := os.FindProcess(pid)
if err != nil {
fmt.Println("read pid process error ")
return
}
err = process.Signal(syscall.SIGINT)
if err != nil {
fmt.Printf("send signal to process error: %s ", err)
return
}
err = os.Remove(config.PID)
if err == os.ErrNotExist {
err = nil
}
if err != nil {
fmt.Printf("send signal to process successfully and remove pid error: %s ", err)
}
fmt.Printf("send signal to process successfully ")
}