Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule time and date to run script

I'm trying to find a package that can do the following in Python:

  • Add/Remove a specified date and time
  • Add/Remove it to a scheduler

What I've looked at:

  • https://docs.python.org/2/library/sched.html
    • Looks like it only does time, not dates.
  • https://pypi.python.org/pypi/python-crontab
    • Does reoccurring days but not a specific date.
like image 366
Lacer Avatar asked Nov 20 '25 14:11

Lacer


1 Answers

You can use sched to do that.

sched.add_job(job_func, trigger='interval', hours=3, start_date='2015-10-10 09:30', end_date='2015-11-10 09:30')

You can pass arguments for trigger. This would set it up to run for 3 hours each day from 2015-10-10 09:30 to until 2015-10-10 09:30. Interval trigger to set up dates. You can also interchange hours with weeks, hours, days, etc. Hope this helps!

Here's some examples using apscheduler.

from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

@sched.scheduled_job('interval', seconds=60)
def timed_job():
    print('This job is run every minute.')

@sched.scheduled_job('cron', day_of_week='mon-fri', hour=17)
def scheduled_job():
    print('This job is run every weekday at 5pm.')

sched.start()
like image 84
Sean Stinehour Avatar answered Nov 22 '25 03:11

Sean Stinehour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!