1
0
Fork 0

Wrote own function to get process pids. Ditching pgrep.

This commit is contained in:
Alexander Andreev 2022-01-02 05:14:21 +04:00
parent d96efaba73
commit d392a3b0b8
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
1 changed files with 32 additions and 0 deletions

32
prog/util.go Normal file
View 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
}