mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
34 lines
822 B
Go
34 lines
822 B
Go
package process
|
|
|
|
import (
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func KillAll(process string, immediately bool) (e error) {
|
|
out, err := exec.Command("sh", "-c", "ps -e -o pid -o comm|grep "+process+"$|awk '{print $1}'").CombinedOutput()
|
|
if err != nil || strings.Contains(string(out), "invalid option") {
|
|
if strings.Contains(strings.ToLower(string(out)), "busybox") {
|
|
out, e = exec.Command("sh", "-c", "ps|awk '{print $1,$5}'|grep "+process+"$|awk '{print $1}'").Output()
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
pids := strings.Split(strings.TrimSpace(string(out)), "\n")
|
|
for _, pid := range pids {
|
|
if len(pid) <= 0 {
|
|
continue
|
|
}
|
|
cmd := "kill "
|
|
if immediately {
|
|
cmd += "-9 "
|
|
}
|
|
cmd += pid
|
|
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
|
|
if err != nil {
|
|
e = newError(string(out)).Base(err)
|
|
}
|
|
}
|
|
return
|
|
}
|