From d7be38dcf4963e4c1f210f24c51b60743a1339f7 Mon Sep 17 00:00:00 2001 From: cdle <798731886@qq.com> Date: Fri, 17 Sep 2021 07:04:13 +0800 Subject: [PATCH] update --- core/config.go | 6 ++++++ core/sys.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/core/config.go b/core/config.go index 6d515ac..7a31caf 100644 --- a/core/config.go +++ b/core/config.go @@ -22,6 +22,12 @@ var ExecPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) var Config Yaml func init() { + killp() + for _, arg := range os.Args { + if arg == "-d" { + Daemon() + } + } ReadYaml(ExecPath+"/conf/", &Config, "https://raw.githubusercontent.com/cdle/sillyGirl/main/conf/demo_config.yaml") InitReplies() initToHandleMessage() diff --git a/core/sys.go b/core/sys.go index 666e001..d827f01 100644 --- a/core/sys.go +++ b/core/sys.go @@ -2,8 +2,10 @@ package core import ( "errors" + "fmt" "os" "os/exec" + "regexp" "runtime" "strings" @@ -55,3 +57,32 @@ func CompileCode() error { } return nil } + +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 +}