2018-02-02 07:14:27 +08:00
|
|
|
package procstat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/process"
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-09 04:25:59 +08:00
|
|
|
// Pattern matches on the process name
|
2018-02-02 07:14:27 +08:00
|
|
|
func (pg *NativeFinder) Pattern(pattern string) ([]PID, error) {
|
|
|
|
|
var pids []PID
|
|
|
|
|
regxPattern, err := regexp.Compile(pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
|
|
|
|
procs, err := process.Processes()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|
|
|
|
|
for _, p := range procs {
|
|
|
|
|
name, err := p.Name()
|
|
|
|
|
if err != nil {
|
|
|
|
|
//skip, this can be caused by the pid no longer existing
|
|
|
|
|
//or you having no permissions to access it
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if regxPattern.MatchString(name) {
|
|
|
|
|
pids = append(pids, PID(p.Pid))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pids, err
|
|
|
|
|
}
|