golang ---查看进程(Windows)
代码:其实主要是调用tasklist和taskkill
package main import ( "fmt" "log" "os/exec" ) func main() { var inputStr string for { fmt.Println("---------------------------------------------------") fmt.Print("1->退出 ") fmt.Print("2->显示进程 ") fmt.Print("3->结束进程 ") fmt.Println("4->显示全部进程") fmt.Println("---------------------------------------------------") fmt.Scanln(&inputStr) if inputStr == "1" { fmt.Println("已退出...") break } else if inputStr == "2" { fmt.Print("请输入进程名称:") var processName string fmt.Scanln(&processName) if len(processName) == 0 { fmt.Println("进程名称不可为空!") } else { text := "imagename eq " + processName + "*" listProcess(text) } } else if inputStr == "3" { fmt.Print("请输入要结束的进程名称:") var processName string fmt.Scanln(&processName) if len(processName) == 0 { fmt.Println("进程名称不可为空!") } else { text := "imagename eq " + processName + "*" killProcess(text) } } else if inputStr == "4" { listAllProcess() } else { fmt.Println("请输入正确的参数!") } } fmt.Scanln(&inputStr) } func listProcess(text string) { fmt.Println(text) cmd := exec.Command("tasklist", "/fi", text) out, err := cmd.CombinedOutput() if err != nil { log.Fatal(err) } fmt.Println(string(out)) } func listAllProcess() { cmd := exec.Command("tasklist") out, err := cmd.CombinedOutput() if err != nil { log.Fatal(err) } fmt.Println(string(out)) } func killProcess(text string) { cmd := exec.Command("taskkill", "/f", "/t", "/fi", text) out, err := cmd.CombinedOutput() if err != nil { log.Fatal(err) } fmt.Println(string(out)) }
相关推荐
GoatSucker 2020-11-23
CoderToy 2020-11-16
蛰脚踝的天蝎 2020-11-10
ZGCdemo 2020-11-09
86193952 2020-10-27
Lzs 2020-10-23
hjr 2020-10-21
HELLOBOY 2020-10-21
qidiantianxia 2020-10-21
cmsmdn 2020-10-21
myspace 2020-10-21
笑面依旧 2020-10-21
专注前端开发 2020-10-21
技术之博大精深 2020-10-16
zhushenghan 2020-11-09
风雨断肠人 2020-10-13
xuguiyi00 2020-11-04
sichenglain 2020-10-27