• 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))
    }

    参考:https://www.cnblogs.com/saryli/p/11652294.html

  • 相关阅读:
    Mysql 基础
    Python Cdn平台文件md5验证
    Linux内存cache/buffer剖析
    wget
    UTV
    智能DNS解析之edns-client-subnet篇
    Python Django
    OpenSSL SNI
    Python 基础(一)
    Python ORM
  • 原文地址:https://www.cnblogs.com/pu369/p/12868187.html
Copyright © 2020-2023  润新知