To execute code every day at a specified time, it is easier and more reliable to use operating system tools such as cron or Windows task scheduler to run a script that performs the action and exits. Otherwise, you have to independently solve such problems as:
- who will restart the script if it dies for any reason (memory is over, unexpected error) - you can run the script as a service (daemon) from under systemd, supervisord, etc
- who will restart the script if it hangs (the process is alive, but does not perform any actions) - a service (watchdog) may be required, which listens to the periodic signals (heartbeat) and restarts the script if it stopped sending them. In turn, someone must ensure that the watchdog service itself is operational, and so on.
If you forget about these problems, you can run the function at a given time using sched.scheduler() :
#!/usr/bin/env python3 import datetime as DT import sched import time DAY = DT.timedelta(1) def some_func(dt): print(dt) # do something dt += DAY s.enterabs(dt.timestamp(), 0, some_func, [dt]) # schedule the next time s = sched.scheduler(time.time) dt = DT.datetime.combine(DT.datetime.today(), DT.time(10, 0)) # today 10:00 s.enterabs(dt.timestamp(), 0, some_func, [dt]) s.run()