0

I want to run a batch file(say wanted to run calculator) on a specific date and time using python (but don't use the default scheduler). My program:

a = "C:\Windows\system32\calc.exe"
mybat = open(r"D:\Cyber_security\Python\cal.bat", "w+")
mybat.write(a)
mybat.close()
import datetime
start = datetime.datetime.now()

end = datetime.datetime(2022, 7, 12, 17, 29, 10)
while(True):
    if(end - start == 0):
        import subprocess
        subprocess.call([r"D:\Cyber_security\Python\cal.bat"])

when I run it it doesn't show error but the batch file is not running at specific time. where is the wrong?

5
  • Please post specific error message (stack trace) Commented Jul 12, 2022 at 11:46
  • 3
    start is never updated. It'll always contain the time when you launched the script, so end - start will be a constant. Commented Jul 12, 2022 at 11:47
  • There is no error message.... Commented Jul 12, 2022 at 11:48
  • 1
    Then ForceBru is correct. 'start' is never updated. In addition, the way it is handled, the cpu clogging will be insane. I suggest adding a 'sleep(5)', then checking if 'end - start <= 0'. Commented Jul 12, 2022 at 11:53
  • Better yet, please view this question, for a better implementation. stackoverflow.com/questions/50121539/… Commented Jul 12, 2022 at 11:54

3 Answers 3

2

Ever tried schedule package?

import schedule
import time

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

schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

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

Comments

0

What about Rocketry?

from rocketry import Rocketry

app = Rocketry()

@app.task('every 1 hour')
def do_hourly():
    ...

@app.task('daily between 10:00 and 14:00 & time of week on Monday')
def do_once_a_day_on_monday():
    ...

@app.task("after task 'do_hourly'")
def do_after_another():
    ...

if __name__ == "__main__":
    app.run()

It also has parametrization, parallelization and Pythonic conditions if you dislike the string-based condition language.

2 Comments

Hello miksus, I've been using your app recently and it's flexibility really amazes me. What I'm trying to figure out now is to run a task at a specific time. Is it possible?
actually my question is if daily runs the task every day? and is it possible to say start running a task at a specific time but with a defined interval??
0

I already found my answer.

a = "C:\Windows\system32\calc.exe"
mybat = open(r"D:\Cyber_security\Python\cal.bat", "w+")
mybat.write(a)
mybat.close()
import sched
from datetime import datetime
import time
def action():
    import subprocess
    subprocess.call([r"D:\Cyber_security\Python\cal.bat"])
s = sched.scheduler(time.time, time.sleep)
e_x = s.enterabs(time.strptime('Tue Jul 12 19:57:17 2022'), 0, action)
s.run()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.