package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
func isProcessExist(appName string) (bool, string, int) {
appary := make(map[string]int)
cmd := exec.Command("cmd", "/C", "tasklist")
output, _ := cmd.Output()
//fmt.Printf("fields: %v\n", output)
n := strings.Index(string(output), "System")
if n == -1 {
fmt.Println("no find")
os.Exit(1)
}
data := string(output)[n:]
fields := strings.Fields(data)
for k, v := range fields {
if v == appName {
appary[appName], _ = strconv.Atoi(fields[k+1])
return true, appName, appary[appName]
}
}
return false, appName, -1
}
func main() {
fmt.Println(isProcessExist("chrome.exe"))
}
输出:
true chrome.exe 7316
查找进程并删除进程
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
func isProcessExist(appName string) (bool, string, int) {
appary := make(map[string]int)
cmd := exec.Command("cmd", "/C", "tasklist")
output, _ := cmd.Output()
//fmt.Printf("fields: %v\n", output)
n := strings.Index(string(output), "System")
if n == -1 {
fmt.Println("no find")
os.Exit(1)
}
data := string(output)[n:]
fields := strings.Fields(data)
for k, v := range fields {
if v == appName {
appary[appName], _ = strconv.Atoi(fields[k+1])
return true, appName, appary[appName]
}
}
return false, appName, -1
}
func main() {
apName := "notepad++.exe"
fmt.Println(isProcessExist(apName))
c := exec.Command("cmd.exe", "/C", "taskkill", "/IM", apName)
err := c.Start()
if err != nil {
fmt.Println(err)
}
c.Wait()
}
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))
}
有关tasklist相关命令请参考:https://www.cnblogs.com/saryli/p/11652789.html
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询