Wrote own function to get process pids. Ditching pgrep.
This commit is contained in:
parent
d96efaba73
commit
d392a3b0b8
32
prog/util.go
Normal file
32
prog/util.go
Normal file
@ -0,0 +1,32 @@
|
||||
package prog
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetProcessPIDs(name string) ([]int, error) {
|
||||
var pids []int
|
||||
|
||||
dir, err := ioutil.ReadDir("/proc/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, entry := range dir {
|
||||
pid, err := strconv.Atoi(entry.Name())
|
||||
if entry.IsDir() && err == nil {
|
||||
f, err := os.ReadFile("/proc/" + entry.Name() + "/comm")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if name == string(f[:len(f)-1]) {
|
||||
pids = append(pids, pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pids, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user