6

I am trying to run something at the exact time to the second everyday. I have tried Schedule and used a sleep time of 1 second but it runs twice sometimes so I want to switch to APScheduler. But I have never used anything Cron like before and their webpage's "User Guide" thing doesn't resemble a detailed documentation at all so I don't know where to start. Googled but there's only answers for intervals. I want to specify a time not interval.

For example run a function at 03:00:05 everyday. How would you do that?

Thank you.

Luluz
  • 63
  • 1
  • 4

3 Answers3

10

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.)

Matteo Zanoni
  • 3,429
  • 9
  • 27
  • I tried to run the code but it returns NameError: name 'function' is not defined – Luluz May 04 '21 at 19:56
  • The scheduler runs the same function 6 times at the specified time, any idea why? – Luluz May 04 '21 at 23:09
  • Would running the same code multiple times to test on jupyter notebook be the cause? scheduler.shutdown() would solve it? – Luluz May 04 '21 at 23:26
  • If you ran the `scheduler.add_job` function multiple times then it would probably be the reason. Each time you run it it would schedule another job. To be sure you could check the length of `scheduler.get_jobs()`. – Matteo Zanoni May 05 '21 at 12:21
  • I have tried it but nothing happens – alper Jun 06 '22 at 22:58
0

I would write my own function.

from time import time, ctime
def your_function():
   pass

if __name__ == "__main__":
    while True:
        now = ctime(time())[11:19]
        if now == "12:34:56":
            try:
                your_function()
                sleep(1.25)
            except:
                pass # or raise
        else:
            sleep(0.75)

This should do the job.

Tanveer
  • 51
  • 1
  • 1
  • 7
-2
apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
The Thonnu
  • 3,578
  • 2
  • 8
  • 30