I believe what you want is the BackgroundScheduler
from APScheduler using a CronTrigger
.
A minimal example of the program would be the following:
from time import sleep
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
def foo(bar):
print(bar)
def main():
scheduler = BackgroundScheduler()
scheduler.start()
trigger = CronTrigger(
year="*", month="*", day="*", hour="3", minute="0", second="5"
)
scheduler.add_job(
foo,
trigger=trigger,
args=["hello world"],
name="daily foo",
)
while True:
sleep(5)
if __name__ == "__main__":
main()
This would run the function foo
with argument bar
equal to the string "hello world" every day at 03:00:05.
The background scheduler is not blocking, therefore the while loop is needed to keep the program running forever.
To change the function to call you would just need to change the first argument of scheduler.add_job
to the function you want to call and change the args
keyword argument to a list containing the arguments you want your function to be called with.
The arguments to CronTrigger
form a cron string, this is a well-known format used by the crontab utility in Linux (i find the following site quite useful for testing those strings.)