34 lines
528 B
Go
34 lines
528 B
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
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() + "/cmdline")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if strings.Contains(string(f), name) {
|
|
pids = append(pids, pid)
|
|
}
|
|
}
|
|
}
|
|
|
|
return pids, nil
|
|
}
|