watchDog.py 1.0 KB

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