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
| func ProcessExists(processName string) (bool, error) { result := false fileInfos, err := ioutil.ReadDir("/proc") if err != nil { return false, err } for _, info := range fileInfos { name := info.Name() matched, err := regexp.MatchString("[0-9]+", name) if err != nil { return false, err } if !matched { continue } cmdLine, err := parseCmdLine("/proc/" + info.Name() + "/cmdline") if err != nil { glog.Error("read cmd line failed ", err) continue } if strings.Contains(cmdLine, processName) { result = true } } return result, err }
func parseCmdLine(path string) (string, error) { cmdData, err := ioutil.ReadFile(path) if err != nil { return "", err } if len(cmdData) < 1 { return "", nil }
split := strings.Split(string(bytes.TrimRight(cmdData, string("\x00"))), string(byte(0))) return strings.Join(split, " "), nil }
|