Search code examples
pythonpython-3.xschedule

Why won't schedule work in this python script?


schedule won't work in this script. The script works just fine without schedule, but the script you see here just wont run, when i run sudo python3 "the script file" it starts to run the script but nothing happends. anyone know why and maybe how to fix it?

import Adafruit_DHT
import requests
import schedule
import time
sensor = Adafruit_DHT.DHT22
pin = 2
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
def jobb():
    print('Kjører script...')
    if humidity is not None and temperature is not None:
        print('Temperature ={0:0.1f}*C  Humidity = {1:0.1f}%'.format(temperature, humidity))
        payload = {'temp': temperature, 'hum': humidity}
        r = requests.post('http://192.168.1.7/test/MySQL_POST_Test.php, data=payload')
        print(r.text)
    else:
        print('Failed to read the sensor, try "sudo python3 sensor-post.py" again.')
schedule.every().day.at("00.00").do(jobb)
schedule.every().day.at("02.00").do(jobb)
schedule.every().day.at("04.00").do(jobb)
schedule.every().day.at("06.00").do(jobb)
schedule.every().day.at("08.00").do(jobb)
schedule.every().day.at("10.00").do(jobb)
schedule.every().day.at("12.00").do(jobb)
schedule.every().day.at("14.00").do(jobb)
schedule.every().day.at("16.00").do(jobb)
schedule.every().day.at("18.00").do(jobb)
schedule.every().day.at("20.00").do(jobb)
schedule.every().day.at("22.00").do(jobb)
while True:
    schedule.run_pending()
    time.sleep(1)

If you have any questions just ask and Ill try to reply as fast as i can.


Solution


  • Important

    I would recommend using a cron job for this sort of thing. The solution proposed here will also work but it's very unstable as it will stop working if the script ever stops and will have to be restarted if the computer is ever turned off.


    When running your script, I get the error

    schedule.ScheduleValueError: Invalid time format

    I googled the time format and changed all the times from

    schedule.every().day.at("08.00").do(jobb)
    

    To

    schedule.every().day.at("08:00").do(jobb)
    

    And it works