''' Description: Version: 1.0 Autor: lishengyin Date: 2022-01-14 10:11:13 LastEditors: lishengyin LastEditTime: 2022-01-14 14:49:21 ''' # 简易UDP看门狗 import time import socket import os def resetMiva(): # 查询PID result = os.popen('pidof main').readlines() if len(result): Ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) os.popen('echo "' + Ctime + ' MIVA卡死" >> ./watchDog.log') # 判定为卡死 os.popen('killall main').readlines() if __name__ == '__main__': # 2、创建套接字 udp_socket_server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) udp_socket_server.settimeout(30) # 3、绑定服务端的IP和端口 udp_socket_server.bind(("",9527)) try: udp_socket_server.settimeout(30) # 4、接收客户端的信息 recv_data,ip_port = udp_socket_server.recvfrom(1024) except socket.timeout: print("超时没收到") resetMiva() # 7、关闭套接字 udp_socket_server.close()