2022-01-02 21:30:56 +04:00
|
|
|
package main
|
2022-01-02 05:14:21 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2022-01-02 21:30:56 +04:00
|
|
|
"strings"
|
2022-01-02 05:14:21 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-02 21:30:56 +04:00
|
|
|
if strings.Contains(string(f[:len(f)-1]), name) {
|
2022-01-02 05:14:21 +04:00
|
|
|
pids = append(pids, pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pids, nil
|
|
|
|
}
|