forked from 764763903a/xdd-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deamon.go
60 lines (54 loc) · 1.01 KB
/
deamon.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
60
package models
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/beego/beego/v2/adapter/logs"
)
func Daemon() {
args := os.Args[1:]
execArgs := make([]string, 0)
l := len(args)
for i := 0; i < l; i++ {
if strings.Index(args[i], "-d") == 0 {
continue
}
execArgs = append(execArgs, args[i])
}
proc := exec.Command(os.Args[0], execArgs...)
err := proc.Start()
if err != nil {
panic(err)
}
logs.Info("小滴滴运行于后台模式")
os.Exit(0)
}
func killp() {
pids, err := ppid()
if err == nil {
if len(pids) == 0 {
return
} else {
exec.Command("sh", "-c", "kill -9 "+strings.Join(pids, " ")).Output()
}
} else {
return
}
}
func ppid() ([]string, error) {
pid := fmt.Sprint(os.Getpid())
pids := []string{}
rtn, err := exec.Command("sh", "-c", "pidof "+pname).Output()
if err != nil {
return pids, err
}
re := regexp.MustCompile(`[\d]+`)
for _, v := range re.FindAll(rtn, -1) {
if string(v) != pid {
pids = append(pids, string(v))
}
}
return pids, nil
}