question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can i run my task between some time?

See original GitHub issue

It seems that the package has not function to execute a task from time to time.

Is there any pythonic method to achieve it?

Like…


import schedule

def task():
    print 'Hello'

schedule.every(5).minutes.between('12:00','24:00').do(task)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:10

github_iconTop GitHub Comments

2reactions
ChuanPoLeecommented, Mar 20, 2019

Hi, @ciroanacleto Although I never use .tag(), but it sounds interesting. It can function like a switch. Here is another example, if you don’t need job to run at exactly every o’clock, try this:

def job():
    print("I'm working %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
def control_job():
    #run between Mon. to Fri. and 09 ~ 18 hour/day
    tnow = datetime.datetime.now()
    hh = tnow.strftime('%H')   #hour like '09'
    wk = tnow.isoweekday() #Mon =1
    if (('08'<hh<'18') and (1 <=wk <=5)):
        job()
    
schedule.every().hour.do(control_job)
1reaction
ChuanPoLeecommented, Mar 20, 2019

ok, I found some way to do this.

import schedule
import time, datetime

def job():
    print("I'm working %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))

def control_job():
    tnow = datetime.datetime.now()
    hm = tnow.strftime('%H:%M')   #hour:minute like '10:00'
    if hm in run_hour:
        if run_hour[hm] == 0:
            #run job and set the run flag to 1
            job()
            run_hour[hm]=1
            #debug
            print(run_hour)
    #time < 10:00 and time> 22:00 reset all flag to 0
    if ((hm <'10:00') or (hm >'22:01')):
        for i in run_hour:
            run_hour[i]=0
    
#global variable, set every hour as a run flag
run_hour = {'10:00':0, '11:00':0, '12:00':0, '13:00':0, '14:00':0, '15:00':0, 
'16:00':0, '17:00':0, '18:00':0, '19:00':0, '20:00':0, '21:00':0, '22:00':0 }

schedule.every(59).seconds.do(control_job)  #set less than 1 min. ensure compare the flag at least once per minute.
while True:
    schedule.run_pending()
    time.sleep(1)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Run Programs Automatically Using Windows Task Scheduler
Go to the Start menu search bar, type in 'task scheduler,' and select the best match. · In the Task Scheduler menu, right-click...
Read more >
How to create an automated task using Task Scheduler on ...
To run a task on demand, right-click it and select the Run option. To edit a task, right-click it and select the Properties...
Read more >
Windows Task Scheduler - Run only during window of time
Set a daily schedule starting at 5pm. In the Advanced dialog, click Repeat task, then specify Every=15 minutes, Until:Time=5am. Edit: The above instructions ......
Read more >
Use the at command to schedule tasks - Windows Client
You can use the at command to schedule a command, a script, or a program to run at a specified date and time....
Read more >
How to Execute a Task Hourly in Task Scheduler - - Tech Rando
First, we go to the task that we want to schedule hourly in Task Scheduler, available in the Task Scheduler Library. For example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found