Reading proc of a linux system

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.

What I know so far is that this line causes the problem. Because of this my program hangs itself.

Edit: One of the libraries I use runs a Goroutine. As soon as I disable the routine, It works fine. But the problem is that I need this routine in some cases.
This is the routine which causes the problem:

func StartElastic() {
	done = false
	wg.Add(1)
	go sendelastic()
}

I solved it on my own. The library had an endless loop without any waiting. I put a sleep into this loop and now it works like a charme.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.