Signaling a process by name instead of pid

Hi. Is there any way to find a process by name and not only by pid, or at least get a list of running processes on the system (Linux) without manually reading /proc and parsing the stats? I’m looking to signal a process but I didn’t find anything similar to os.FindProcess that takes a name. I can just read /proc in the end but I didn’t want to do this before I was sure there isn’t any interface related to this.

Hey @alectic, you should check out this small library I wrote a little while back.

It includes a function called FindByName. Underneath it uses the ps command since I am running a Mac and /proc isn’t available on my computer.

https://github.com/radovskyb/process.

P.s If you want to see an example of it being used, check out gobeat.
https://github.com/radovskyb/gobeat.

Hi @radovskyb, thanks for replying. Unfortunately calling an external utility is not what I’m looking for in this case. I would prefer to have a direct interaction with the operating system and avoid calling or depending on external tools for such things.

Yeah, I get what you mean…

Have a look at https://github.com/mitchellh/go-ps. It might be a good starting point for you. I don’t think there’s find by name functionality in it yet, but I can’t imagine it would be too hard to add with something like below as a starting point. You could probably even take the code from process's FindByName function and swap out the ps calling parts for something like below.

package main

import (
	"fmt"
	"log"

	ps "github.com/mitchellh/go-ps"
)

func main() {
	procs, err := ps.Processes()
	if err != nil {
		log.Fatalln(err)
	}

	for _, proc := range procs {
		fmt.Println(proc.Executable())
	}
}

Indeed, it’s a good starting point. I stumbled upon go-ps earlier when I was googling for this but thought to ask before if there’s any alternative in the std. This seems to be the best bet right now so I’ll just parse /proc.

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