监控win10程序
Windows10的机器上启动了两个应用,时不时会挂了,所以得在win10上搞个定时程序定时监控一下。
1、实现
1、代码
Golang
package main
import (
"fmt"
"github.com/shirou/gopsutil/process"
"log"
"net/http"
"os/exec"
"strings"
"time"
)
type Dingding struct {
Dingurl string
}
func (d Dingding) SendMsgbyText(msg string) {
content := `{"msgtype": "text",
"text": {"content": "` + msg + `"}
}`
req, _ := http.NewRequest("POST", d.Dingurl, strings.NewReader(content))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
}
type winProcess struct {
name string
path string
}
func (ppp winProcess) check_process_status() (bool,int32){
processes, _ := process.Processes()
for _, p := range processes {
name,_ := p.Name()
if name == ppp.name {
status,_ := p.IsRunning()
return status, p.Pid
}
}
return false, int32(-1)
}
func (ppp winProcess) start_process() {
sd := Dingding{"xxxx"}
sd.SendMsgbyText(ppp.name + " 应用启动了!!!")
cmd := exec.Command("cmd", "/c", ppp.path)
err := cmd.Start()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
func (ppp winProcess) process_control() {
if status, _ := ppp.check_process_status() ; ! status {
ppp.start_process()
fmt.Printf("%s 进程启动了!!!",ppp.name)
}else {
fmt.Printf("%s 程序正在运行!!!", ppp.name)
}
}
func (ppp winProcess) process_stop() {
c := exec.Command("taskkill.exe", "/f", "/im", ppp.name)
err := c.Start()
if err != nil {
fmt.Println("有错误")
}
}
func main() {
//先启动软平台 再启动接口程序
abc := winProcess{
"abc.exe",
"C:\\Users\\process_check\\abc_start.bat",
}
abc.process_control()
time.Sleep(15 * time.Second)
de := winProcess{
"CTItoCRMPlate.exe",
"start E:\\abc\\abc.exe",
}
de.process_control()
}
2、windows文件路径
疑问???为啥另一个进程没有单独用bat的脚本处理呢?上面的Program Files 这个中间有空格,放在golang里面调用,win10只识别到了program,后面就断开了。
2、配置定时程序
1、windows的cronjob
2、如果不是只在用户登录时运行,默认就放到后台了。
3、每5分钟一次
4、根目录得选,避免傻逼问题。
本文阅读量 次