0

I am running a python script to check the internet connection of a wifi network. It is being run for every 10minutes. Afwer a one month of working, the scheduler is not invoking the actual function due to the following reason.

2019-08-27 14:13:27,259 : Next wakeup is due at 2019-08-27 14:23:27.254425+00:00 (in 599.996401 seconds) 2019-08-27 14:23:27,257 : Looking for jobs to run 2019-08-27 14:23:27,258 : Execution of job "checkwifi (trigger: interval[0:10:00], next run at: 2019-08-27 14:23:27 UTC)" skipped: maximum number of running instances reached (1) 2019-08-27 14:23:27,259 : Next wakeup is due at 2019-08-27 14:33:27.254425+00:00 (in 599.996381 seconds) 2019-08-27 14:33:27,257 : Looking for jobs to run 2019-08-27 14:33:27,258 : Execution of job "checkwifi (trigger: interval[0:10:00], next run at: 2019-08-27 14:33:27 UTC)" skipped: maximum number of running instances reached (1)

When I search about this error, I found that this happens only when the script hang for more than 10 minutes, But in my script i didn't find any reason to hang the script for 10minutes or more. The main problem is that the background scheduler is not recovering from this problem. Actually if this happens it should run the next instance after completing the hanging instance but this is not happening here. So we have to assume that the script is hang for 2 days.

Is it possible to hang a script for 2 days?

The script is given below.

import commands as cm
import netifaces as ni
import requests,socket,logging
from logging.handlers import RotatingFileHandler
import signal
from apscheduler.schedulers.background import BackgroundScheduler



log_formatter = logging.Formatter('%(asctime)s : %(message)s')
logFile = 'output.log'

rotate_handler = RotatingFileHandler(logFile, mode='a', maxBytes=1024*1024, 
                                 backupCount=1)
rotate_handler.setFormatter(log_formatter)

Logging = logging.getLogger()
Logging.setLevel(logging.DEBUG)
Logging.addHandler(rotate_handler)

print "ddd"

def checkwifi():
    URL="http://webauth-wlc.lacounty.co/login.html"
    checkURL="https://www.google.co.in/"

    d={}
    d["buttonClicked"]='4'
    d["redirect_url"]="http://dhs.lacounty.gov/wps/portal/dhs"
    d["err_flag"]='0'

    net=''
    port=53
    host="8.8.8.8"
    timeout=90
    i=1
    restartwifi=0

    while i<=4:

        try:
            Logging.debug('Checking Internet Try : %s'%i)
            ssid=cm.getoutput('iwgetid -r')
            IPaddr=ni.ifaddresses('wlan0')[ni.AF_INET][0]['addr']
        except Exception as e:
            Logging.debug('Read error in ssid and Ipaddress')
            Logging.debug(repr(e))
            IPaddr=''

        Logging.debug("SSID : "+ssid)
        Logging.debug("IP : "+IPaddr)

        try:
            socket.setdefaulttimeout(timeout)
            socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
            r=requests.get(checkURL,timeout=timeout)
            sure="""<html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN">"""

            if sure in r.text:
                Logging.debug("Getting google.com")                             ## validate coz it may redirect to captive portal login page.
                net=True

            else:
                Logging.debug("Not getting google.com")
                net=False

        except Exception as ex:
            net=False

        if net==False:
            Logging.debug("NO INTERNET")
            if ssid=='DHS_Guest':
                Logging.debug('Giving request to DHS_Guest for internet')
                try:
                    r=requests.post(URL,data=d)
                    Logging.debug("DONE : status_code = %s"%r.status_code)
                except Exception as e:
                    Logging.debug("Error Occured : \n"+repr(e))
                    restartwifi=1

            else:
                if ssid:
                    Logging.debug("STATUS : network is not DHS_Guest")
                    break               ##if connected other network break
                else:
                    restartwifi=1
                    Logging.debug("STATUS : Not connected to any network")

        elif net==True:
            Logging.debug("STATUS : ALREADY CONNECTED TO INTERNET")
            break

        if restartwifi==0:
            try:
                socket.setdefaulttimeout(timeout)
                socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
                r=requests.get(checkURL,timeout=timeout)
                sure="""<html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN">"""
                if sure in r.text:
                    Logging.debug("STATUS : INTERNET CONNECTED")
                    net=True
                    break
                else:
                    Logging.debug("STATUS : NOT SUCCESSFUL")
                    net=False

            except Exception as ex:
                net=False
                Logging.debug("STATUS : NOT SUCCESSFUL")



        if i==4:
            break


        if i==3 or restartwifi==1:
            restartwifi=0
            Logging.debug("Restarting wlan0")
            try:
                status1=cm.getstatusoutput('sudo systemctl daemon-reload')
                status2=cm.getstatusoutput('sudo systemctl restart networking.service')
                status3=cm.getstatusoutput('sudo ifdown wlan0')
                status3=cm.getstatusoutput('sudo ifup wlan0')
                Logging.debug("%s,%s,%s"%(status1[0],status2[0],status3[0]))
            except Exception as e:
                Logging.debug("Restarting ERROR : "+repr(e))



        i=i+1
        net=''
    return net

if __name__=="__main__":
    try:
        checkwifi()
        doit=BackgroundScheduler()
        doit.add_job(checkwifi,'interval',minutes=13,id='caportal')
        doit.start()
        signal.pause()
    except Exception as e:
        Logging.debug("Function call ERROR : "+repr(e))

How to solve this issue?

I have got one clue that. This error happened for two times, for each time I rebooted the rpi to bring it back to normal state. When i observe the log file the last print is at requests.get(checkURL,timeout=timeout) and the print is Starting new HTTPS connection (1): www.google.co.in.

So I think the program hangs on requests object. What are the chances to hang the requests even if the timeout is given?

  • what happens if you repeatedly run the script from command line? – jsotola Oct 7 at 5:46
  • it's run fine. Actually, the script is working for one month. I didn't understand why that happens.? – mcv Oct 7 at 6:07
  • Do not ask questions about obsolete OS – Milliways Oct 7 at 6:32
  • why is the date wrong? – jsotola Oct 7 at 6:35
  • 1
    @Milliways If the OS is obsolete, It doesn't mean that it is not usable. – mcv Oct 7 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.