-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.go
40 lines (35 loc) · 1.1 KB
/
pid.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
package main
import (
"os"
"os/exec"
"strings"
)
func findAllPid() []string {
cmd := exec.Command("cmd", "/C", "tasklist")
output, _ := cmd.Output()
// output 是类似下面这样的字符串的byte,从System开始的字符串才是有用的
// 映像名称 PID 会话名 会话# 内存使用
//========================= ======== ================ =========== ============
//System Idle Process 0 Services 0 8 K
//System 4 Services 0 2,028 K
// 获取System出现的位置
n := strings.Index(string(output), "System")
if n == -1 {
Info.Println("No Find")
os.Exit(1)
}
// 切割字符串接下来都是 System 4 Services 0 2,028 Kddde 一行一行的数据
data := string(output)[n:]
fields := strings.Fields(data)
return fields
}
func killProcess(appName string) bool {
command := exec.Command("TASKKILL", "/F", "/T", "/IM", appName)
output, err := command.Output()
if err != nil {
Error.Println(err)
return false
}
Error.Println(string(output))
return true
}