Python script to do something at the same time every day

Paul McKenzie picture Paul McKenzie · Feb 26, 2013 · Viewed 225.7k times · Source

I have a long running python script that I want to do someting at 01:00 every morning.

I have been looking at the sched module and at the Timer object but I can't see how to use these to achieve this.

Answer

user2099484 picture user2099484 · May 22, 2015

I spent quite a bit of time also looking to launch a simple Python program at 01:00. For some reason, I couldn't get cron to launch it and APScheduler seemed rather complex for something that should be simple. Schedule (https://pypi.python.org/pypi/schedule) seemed about right.

You will have to install their Python library:

pip install schedule

This is modified from their sample program:

import schedule
import time

def job(t):
    print "I'm working...", t
    return

schedule.every().day.at("01:00").do(job,'It is 01:00')

while True:
    schedule.run_pending()
    time.sleep(60) # wait one minute

You will need to put your own function in place of job and run it with nohup, e.g.:

nohup python2.7 MyScheduledProgram.py &

Don't forget to start it again if you reboot.