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
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
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
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 minutebut 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
https://schedule.readthedocs.io/en/stable/index.htmland it's seem to be working
