-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
process_linux.go
59 lines (46 loc) · 986 Bytes
/
process_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package process
/*
References:
https://github.com/mitchellh/go-ps/
type Process interface {
Pid() int
PPid() int
Executable() string
}
*/
import (
"errors"
ps "github.com/mitchellh/go-ps"
)
func GetProcesses() ([]ps.Process, error) {
processes, err := ps.Processes()
if err != nil {
return processes, err
}
return processes, nil
}
func FindPidByName(name string) ([]int, error) {
var pids []int
processes, err := ps.Processes() // Get all processes
if err != nil {
return pids, err
}
for _, proc := range processes { // Iterate over them
if proc.Executable() == name { // Check given binary name
pids = append(pids, proc.Pid()) // Append the pid
}
}
return pids, nil
}
func FindNameByPid(pid int) (string, error) {
processes, err := ps.Processes()
if err != nil {
return "", err
}
for _, proc := range processes {
if proc.Pid() == pid {
return proc.Executable(), nil
}
}
return "", errors.New("Pid not found!")
}