Hi,
I am trying to read the proc of a linux filesystem, more specific the comm file.
My problem is, that my binary is stuck at some process and doesn’t do anything anymore.
i tried it with the following library “github.com/mitchellh/go-ps”
processes, err := gops.Processes()
if err != nil {
log.Fatal(err)
}
for _, process := range processes {
if process.Executable() == config.Cmd.Processname {
running = true
log.Debug("Process running: " + strconv.FormatBool(running))
return running
}
}
return running
or oldschool
files, _ := ioutil.ReadDir("/proc")
for _, f := range files {
// check if folder is a integer (process number)
if _, err := strconv.Atoi(f.Name()); err == nil {
// open status file of process
f, err := os.Open("/proc/" + f.Name() + "/status")
if err != nil {
log.Info(err)
return running
}
// read status line by line
scanner := bufio.NewScanner(f)
// check if process name in status of process
for scanner.Scan() {
re := regexp.MustCompile("^Name:\\s*" + config.Cmd.Processname + ".*")
match := re.MatchString(scanner.Text())
if match == true {
running = true
log.Debug("Process running: " + strconv.FormatBool(running))
}
}
if running == true {
return running
}
}
}
I am glad for every suggestion.