-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.go
More file actions
142 lines (125 loc) · 2.64 KB
/
processes.go
File metadata and controls
142 lines (125 loc) · 2.64 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package processes
import (
"fmt"
"os/exec"
"runtime"
"strings"
"time"
)
type Process struct {
Cmd *exec.Cmd
}
func Command(exe string, args ...string) (*Process, error) {
command := exec.Command(exe, args...)
errChan := make(chan error)
go func() {
if err := command.Start(); err != nil {
errChan <- err
}
}()
for {
select {
case <-time.After(time.Millisecond * 50):
return &Process{Cmd: command}, nil
case err := <-errChan:
return nil, err
}
}
}
func ProcessSnapshot() (map[string]string, error) {
if runtime.GOOS == "windows" {
return processSnapshotWindows()
}
return processSnapshotLinux()
}
func GetPid(serverName string) (string, error) {
snapshot, err := ProcessSnapshot()
if err != nil {
return "", err
}
for k, v := range snapshot {
if strings.Index(v, serverName) != -1 {
return k, nil
}
}
return "", fmt.Errorf("NOT FUND")
}
func KillByPid(pid string) error {
if runtime.GOOS == "windows" {
return killPidWindows(pid)
}
return killPidLinux(pid)
}
func killPidLinux(pid string) error {
_, err := RunCommand(fmt.Sprintf("kill -9 %s", pid))
return err
}
func killPidWindows(pid string) error {
c := fmt.Sprintf(`taskkill /F /pid %s`, pid)
_, err := RunCommand(c)
return err
}
func processSnapshotLinux() (map[string]string, error) {
rp := map[string]string{}
a := `ps -ef |awk '{print $2,$8}'`
command, err := RunCommand(a)
if err != nil {
return nil, err
}
split := strings.Split(command, "\n")
for _, v := range split {
sp2 := strings.Split(v, " ")
if len(sp2) >= 2 {
rp[sp2[0]] = sp2[1]
}
}
return rp, nil
}
func processSnapshotWindows() (map[string]string, error) {
rp := map[string]string{}
a := "tasklist"
command, err := RunCommand(a)
if err != nil {
return nil, err
}
split := strings.Split(command, "\n")
for _, v := range split {
sp2 := strings.Split(v, " ")
sp2 = clearNone(sp2)
if len(sp2) >= 2 {
rp[sp2[1]] = sp2[0]
}
}
return rp, nil
}
func runInWindows(cmd string) (string, error) {
result, err := exec.Command("cmd", "/c", cmd).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(result)), err
}
func RunCommand(cmd string) (string, error) {
if runtime.GOOS == "windows" {
return runInWindows(cmd)
} else {
return runInLinux(cmd)
}
}
func runInLinux(cmd string) (string, error) {
//fmt.Println("Running Linux cmd:" + cmd)
result, err := exec.Command("/bin/sh", "-c", cmd).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(result)), err
}
func clearNone(sp []string) []string {
var result []string
for _, v := range sp {
if v != "" {
result = append(result, v)
}
}
return result
}