Hello,
I have a simple function that I want it to run every 1 min
the function check internet connection - ping to google (8.8.8.8)
this is all the code
but when I run the code it only run me 1 time , and not every 60 seconds
what did I miss in my understanding of "threading.Timer"?
*** it there is a better\simple way to tp make it run once every x min - show me please ,
Thanks,
I have found and used this
and it's seem to be working
I have a simple function that I want it to run every 1 min
the function check internet connection - ping to google (8.8.8.8)
this is all the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import os import sys import time import datetime import requests import threading import socket import netifaces as ni Error_Count = 0 def get_ip_address(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(( "8.8.8.8" , 80 )) return s.getsockname()[ 0 ] def CheckConnection(): #threading.Timer(60.0, CheckConnection).start() # called every minute global Error_Count address_check = '8.8.8.8' response = os.system( 'ping -c 2 ' + address_check) if response = = 0 : pingstatus = "Network Active" print (pingstatus, "count: " , str (Error_Count)) else : if Error_Count = = 3 : #os.system('sudo shutdown -r now') print ( 'put here reboot command if nothing works , need to check remove route maybe' ) Error_Count = 0 else : errorTime = datetime.datetime.now() pingstatus = ( str (errorTime) + " : Netwrok Down!" ) OutInterface = get_ip_address() print ( 'trying to go out using : ' + OutInterface) time.sleep( 2 ) boot_response = os.system( 'sudo sh /bin/modemstart > /home/pi/logs/modemstart.txt 2>&1 &' ) print ( str (boot_response) + '\n\r Modem shoud be up , see IP' ) time.sleep( 2 ) # ni.ifaddresses('wwan0') try : Wan_ip = ni.ifaddresses( 'wwan0' )[ni.AF_INET][ 0 ][ 'addr' ] except ValueError as ve1: print (ve1) OpenVpnResponse = os.system( 'sudo openvpn /etc/openvpn/client.conf &' ) print ( str (OpenVpnResponse) + '\n\r VPN should be UP , check it' ) try : Vpn_ip = ni.ifaddresses( 'tun0' )[ni.AF_INET][ 0 ][ 'addr' ] except ValueError as ve: print (ve) Error_Count + = 1 return pingstatus threading.Timer( 60.0 , CheckConnection).start() # called every minute |
what did I miss in my understanding of "threading.Timer"?
*** it there is a better\simple way to tp make it run once every x min - show me please ,
Thanks,
I have found and used this
1 |
https: / / schedule.readthedocs.io / en / stable / index.html |