-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemctl_linux.go
59 lines (52 loc) · 1.42 KB
/
systemctl_linux.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
58
59
package service
import (
"strconv"
"strings"
)
const systemctlExec = "systemctl"
type systemctl struct {
}
func (s systemctl) startCmd(sName string) []string {
return []string{systemctlExec, "start", sName}
}
func (s systemctl) statusCmd(sName string) []string {
return []string{systemctlExec, "status", sName}
}
func (s systemctl) stopCmd(sName string) []string {
return []string{systemctlExec, "stop", sName}
}
func (s systemctl) parseStatus(sData string, err error) (Status, error) {
var st Status
lines := strings.Split(sData, "\n")
if err != nil {
errString := err.Error()
idx := strings.LastIndex(errString, " ")
exitCode, _ := strconv.Atoi(errString[idx+1 : len(errString)])
// SystemD status is 3 when service is stopped or does not exists
if exitCode != 3 {
return st, err
}
for _, line := range lines {
line = strings.Trim(line, " ")
// When services does not exists then "Loaded" contains "not-found"
if strings.HasPrefix(line, "Loaded") && strings.Contains(line, "not-found") {
return st, err
}
}
}
for _, line := range lines {
line = strings.Trim(line, " ")
if strings.HasPrefix(line, "Active") {
st.Running = strings.Contains(line, "active (running)")
if !st.Running {
break
}
} else if strings.HasPrefix(line, "Main PID") {
pid := line[10:len(line)]
idx := strings.Index(pid, " ")
pid = pid[0:idx]
st.PID, _ = strconv.Atoi(pid)
}
}
return st, nil
}