Operation and maintenance of automation scripts -python

Ready to work

1. Create a service, the service name: playgame

https://pypi.org/project/srvwrapper/

安装srvwrapper

pip install srvwrapper

Prepare an infinite loop code ---play_game.py, code is as follows:

import time

while True:
    print("你好")

File Path: D: \ test1 \ dingding \ play_game.py

 

Administrator DOS window open, the command is as follows:

srvwrapper playgame python --arguments "\"D:\test1\dingding\play_game.py""

 

 

 

 

 

psutil Import 

# MEM = psutil.virtual_memory () 
# mem_dict = {} 
# mem_dict [ " the total number of Memory " ] = STR (mem.total/ 1000000000 ) + ' G ' 
# mem_dict [ " memory used " ] = STR (MEM. Used / 1000000000 ) + ' G ' 
# mem_dict [ " available memory " ] = STR (mem.free/ 1000000000 ) + ' G ' 
# mem_dict [ " accounting " ] = mem.percent 

 
Import OS
Import Requests
import json
import time

time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
monitor_name = {"ServiceWrapper.exe"}
headers = {"Content-Type": "application/json; charset=utf-8"}
url = "https://oapi.dingtalk.com/robot/send?access_token=d3d68aaab32efae5ea389949a9b604b080da0b265f5aacf4e0a82e8e97d557b6"
monitor_map = {
    "ServiceWrapper.exe": "net start playgame"
}
while True:
    proc_dict = {}
    proc_name = set()

    for p in psutil.process_iter(attrs=['pid', 'name']):
        proc_dict[p.info['pid']] = p.info['name']
        proc_name.add(p.info['name'])

    proc_stop = monitor_name - proc_name

    if proc_stop:
        for p2 in proc_stop:
            p_status = "停止"

            p_name = p2
            data = {
                "msgtype": "markdown",
                "markdown": {
                    "title": "监控讯息",
                    "text": "%s \n" % time_now +
                            "> #### service name:% S \ the n-\ the n- " % p_name +
                             " > #### state:% S \ the n- " % p_status +
                             " > #### is trying to start " 
                }, 
            } 

            send_data = json .dumps (Data) .encode ( " UTF-. 8 " ) 
            requests.post (URL = URL, Data = the send_data, headers = headers) 
            # execute system commands 
            the os.system (monitor_map [P2]) 
            proc_name = SET () 

            for P3 in psutil.process_iter (attrs = [ ' PID', 'name']):
                proc_name.add(p3.info['name'])

            if p2 in proc_name:
                p_status = "启动"
                data = {
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "监控讯息",
                        "text": "%s \n" % time_now +
                                ">#### 服务名:%s \n\n" % p_name +
                                ">#### 状态:%s \n" % p_status +
                                ">#### 启动成功"
                    },
                }
                send_data = json.dumps(data).encode("utf-8")
                requests.post(url=url, data=send_data, headers=headers)
            else:
                p_status = "停止"
                data = {
                    "msgtype": "markdown",
                    "markdown": {
                        "title": "监控讯息",
                        "text": "%s \n" % time_now +
                                ">#### 服务名:%s \n\n" % p_name +
                                ">#### Status:% S \ n- " % p_status +
                                ">#### 启动失败"
                    },
                }
                send_data = json.dumps(data).encode("utf-8")
                requests.post(url=url, data=send_data, headers=headers)
        time.sleep(5)

 

Guess you like

Origin www.cnblogs.com/aqiuboke/p/11310596.html