DEV Community

Shun Yamada
Shun Yamada

Posted on

How to add cron job in Python

While I was searching for some good ideas to run cron job in Python and eventually I've found APScheduler.

APScheduler has three built-in scheduling systems you can use:

  • Cron-style scheduling (with optional start/end times)
  • Interval-based execution (runs jobs on even intervals, with optional start/end times)
  • One-off delayed execution (runs jobs once, on a set date/time)

This time, I tried to get via API every 10 minutes. So APScheduler definitely holds what I want to realize. In addition to this, it's quite useful because there're some methods which start the scheduler for each environment and each framework.

Install APScheduler

$ pip install apscheduler
Enter fullscreen mode Exit fullscreen mode


def hello_world():
  print("Hello World!")

sched = BackgroundScheduler(standalone=True,coalesce=True)
sched.add_job(hello_world, 'interval', minutes=1)
Enter fullscreen mode Exit fullscreen mode

Print "Hello World!" every 10 minutes.


import app.schedule
Enter fullscreen mode Exit fullscreen mode

Top comments (0)