4

The idea is to keep this script running from 9AM to 6PM.

I want to run the script 'once' 10 seconds before the end of every minute. Meaning 9:01:50,9:02:50,9:03:50 and so on ...till 5:59:50

Here is my code for that

from datetime import datetime, time, timedelta, date
import time


i = 0
while i <= 1:
    t = datetime.now()
    my_time = t.strftime("%H:%M:%S.%f")

    if t.second  >=50:
        print("my local time is", my_time)

        tt = datetime.now()
        time.sleep(55)
        print("after sleep print time is", tt)

Now during the middle of the day if I decide that I want to run this script only one ever half an hour I use

if t.minute == 29 and t.second >=50:
   print("my local time is", my_time)
elseif t.minute == 59 and t.second >=50):
   print("my local time is", my_time)

The problem starts when during the day I change my mind over and over and I decide to run the script every 4 minutes or lets say 6 mintues keeping the start point as 9am and end point as 6pm.

Is there any way where I can set a variable at the beginning of the script such that all I need to do is change that variable and restart the script such that it does the job at the set interval henceforth. So at 9:37am I change the variable to 5 ... where 5 is minutes. then the script will run at 9:39:50, 9:44:50,9:49:50 and so on...

At 10:04 I will change the variable to 15 and the script will make the first print at 10:14:50... the next at 10:29:50 and so on...

How can this be done? Thanks in advance.

3
  • 2
    In case you are running on Linux or MacOS, are you aware of cron/crontab? Commented Aug 24, 2020 at 18:57
  • I am on a windows machine. and its not that i want to run this script individually. this is a function. Commented Aug 24, 2020 at 19:03
  • @J-Jillu check out scheduled tasks: stackoverflow.com/questions/7195503/… Commented Aug 24, 2020 at 19:05

2 Answers 2

2

You probably want the schedule module: https://github.com/dbader/schedule

import schedule
import datetime

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

def office_hours():
    d = datetime.datetime.now()
    return (d.hour > 8) and (d.hour < 18)

interval = 10  # minutes
schedule.every(interval).minutes.at(":50").do(job)

while True:
    if office_hours():
      schedule.run_pending()
      time.sleep(1)
    else:
      time.sleep(60)
Sign up to request clarification or add additional context in comments.

Comments

1

For sure a cron job would be the best for your request. But if you want you can try by defining a function and passing a variable:

from datetime import datetime, time, timedelta, date
import time

def myfunction(interval=1):
    i = 0
    minut_to_run =[] 
    minutes = 59
    while minutes >= 0:
       minut_to_run.append(minutes)
       minutes -= interval

    while i <= 1:
       t = datetime.now()
       my_time = t.strftime("%H:%M:%S.%f")

       if t.second >= 50 and t.minute in minut_to_run:
           print("my local time is", my_time)
           time.sleep(55)
           print("after sleep print time is", t)

interval_in_minut = 6
myfunction(interval=interval_in_minut)

1 Comment

I forgot an e to .minute. I corrected it and it seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.