2

I want to schedule a job (run a python script) everyday at a specific time till a specific date has been reached. Researching on a lot of Pythonic schedulers, I thought that APScheduler was a good candidate to get around this. This is an example snippet using APScheduler that starts a job and executes it every two hours after a specified date.

from datetime import datetime

from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler()
sched.start()

def job_function():
    print "Hello World"

# Schedule job_function to be called every two hours
sched.add_interval_job(job_function, hours=2)

# The same as before, but start after a certain time point
sched.add_interval_job(job_function, hours=2, start_date='2010-10-10 09:30')

How to achieve the same and have a upper limit date after which the job should not be executed? Any suggestions that revolve within and outside the APScheduler are most welcome. Thanks in advance.

Tania
  • 1,855
  • 1
  • 15
  • 38
  • 1
    2010 already happened – Ryan Haining Jun 09 '15 at 05:09
  • 4
    I'm tempted to say that this would likely be better solved by the system and not the python script – Ryan Haining Jun 09 '15 at 05:10
  • A cron job? It would be better if the script can schedule jobs so it would require less manual intervention :) Any suggestions, @RyanHaining? – Tania Jun 09 '15 at 05:17
  • 1
    it wouldn't be that hard to write a python script that calls a function, then waits two hours, then checks the date (exiting if its too late), then calls the function again. My concern would be that you couldn't update python or any of the code being used without restarting the python script, you'd have to handle and recover from ANY exception that occurred, and you'd still need something in the system to run the script every time the system reboots. – Ryan Haining Jun 09 '15 at 05:23
  • I'd definitely use cron for this. You don't have to worry about the process running continuosly. You don't have to monitor it or anything else. Just ask cron to run your script once a day and on the last day, either remove the entry from cron or rename the script so that it won't run again. – Noufal Ibrahim Jun 09 '15 at 05:24
  • Yeah it is simple (and the question is a duplicate): http://stackoverflow.com/questions/11774925/how-to-run-a-python-file-using-cron-jobs – Tom Jun 09 '15 at 05:24
  • @Tom: This is not a duplicate of that question. I am just concerned about setting the upper limit for a scheduler. The other question deals with executing a python script at regular intervals which is already done with APScheduler :) – Tania Jun 09 '15 at 05:27
  • @NoufalIbrahim and Ryan: The reason why I chose having a scheduler function than a cronjob is because the jobs that needs to be scheduled may come dynamically. And I have to add and remove them from cron programmatically. Can you please guide me on how to achieve this? – Tania Jun 09 '15 at 05:29
  • A common approach is to have the job in a file, and remove or rename the file out of the way programmatically when the time comes. – tripleee Jun 09 '15 at 05:30

2 Answers2

2

Use a cron job that executes your script every two hours (cron is made specifically for things like this). In your script, you just look up the system date and check, if it's smaller than your given date. If it's smaller, you execute the rest of your script, otherwise you quit.

You may also write additional code, so you get notified when the script is not actually executed anymore.

Exceen
  • 765
  • 1
  • 4
  • 20
1

I eventually found the interval trigger can take an end_date.

You can pass arguments for the trigger to add_job with trigger='interval':

sched.add_job(job_function, trigger='interval', hours=2, end_date='2016-10-10 09:30')

I think you may be using an older version of the software.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Is it possible to name a job and store it somewhere so duplicate jobs within the same end date cant be created? – Tania Jun 10 '15 at 07:40
  • @Tania I have no idea. I've never used the software, and only found this answer for you by careful searching of the documentation. – Peter Wood Jun 10 '15 at 07:48