go windows进程名获取进程id

func findProcessID(processName string)(int,error){
   buf:=bytes.Buffer{}
   cmd:=exec.Command("wmic","process","get","name,processid")
   cmd.Stdout =&buf
   cmd.Run()

   cmd2 :=exec.Command("findstr",processName)
   cmd2.Stdin =&buf
   data,_:=cmd2.CombinedOutput()
   if len(data) ==0 {
      return -1,errors.New("not find")
   }

   info:=string(data)
   reg:=regexp.MustCompile(`[0-9]+`)

   pid:=reg.FindString(info)

   return strconv.Atoi(pid)
}
发布了63 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_33048069/article/details/103798356