Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduling jobs using crontab syntax #283

Open
yuanshi1602 opened this issue Jan 30, 2019 · 6 comments
Open

Scheduling jobs using crontab syntax #283

yuanshi1602 opened this issue Jan 30, 2019 · 6 comments

Comments

@yuanshi1602
Copy link

0,15,30,45 * * * *, how to do it

@connorskees
Copy link
Contributor

import schedule
import time

def job():
    print("I'm working...")

schedule.every(15).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

@mojeto
Copy link

mojeto commented Jan 31, 2019

crontab 0,15,30,45 * * * * isn't fully supported by this library out of the box.
Jobs get scheduled relative to the scheduler process start time . (applies for minutes and hours job units)
That means your job will run every 15 minutes, but with some offset to the actual time.
To run your jobs at exact 0, 15, 30 and 45 minutes of each hour you have to schedule jobs using days unit.

from itertools import product
step = 15  # every x minutes
for hour, minute in product(range(0, 24), range(0, 60, step)):
    time = "{hour:02d}:{minute:02d}".format(hour=hour, minute=minute)
    schedule.every().day.at(time).do(job)

or you have to start scheduler process exactly at full hour so the jobs offset to the actual time is zero.

@mojeto
Copy link

mojeto commented Jan 31, 2019

Simplified solution based on #248 (comment) is to use hour unit with minute offset

step = 15  # every x minutes
for minute in range(0, 60, step):
    time = ":{minute:02d}".format(minute=minute)
    schedule.every().hour.at(time).do(job)

@dkgitdev
Copy link

dkgitdev commented Jun 8, 2019

import schedule
import time

def job():
    print("I'm working...")

schedule.every(15).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

In order to make it run in exact times, you can have it waiting until some of your minutes like
15,30,45,0 and execute the quoted script at that time

@edatpb
Copy link

edatpb commented Jan 7, 2020

I would really like to upvote this as a feature request

I have a task that I want to run every day at 2AM and 2PM, which in crontab would be 0 2,14 * * * (which is not readable, not python, and all the other reasons I like schedule over cron)

In schedule this does what I want:

for t in ["02:00", "14:00"]:
    schedule.every().day.at(t).do(task)

but it doesn't look the way I want. If I now go to introspect schedule.jobs it looks like I have two jobs, when really I just want to see one. For most applications I would be interested in the kinds of job I'm running (i.e. the names of each callable, or whatever), so I don't want to see it repeated unnecessarily. The whole point of the module is to make automated tasks human-readable. I would argue that this:

[
    Every 1 day at 02:00:00 do task(),
    Every 1 day at 14:00:00 do task(),
]

is less human-readable than something like this:

[
    Every 1 day at 02:00:00 and 14:00:00 do task()
]

@DGMaxime
Copy link

I also upvote for this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants