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?

ijahan
  • 93
  • 1
  • 9

3 Answers3

1

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)
Devyl
  • 565
  • 3
  • 8
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.

miksus
  • 2,426
  • 1
  • 18
  • 34
  • 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? – Hossein Gholami May 23 '23 at 09:05
  • 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?? – Hossein Gholami May 23 '23 at 10:26
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()
ijahan
  • 93
  • 1
  • 9
  • 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](/help/how-to-answer). – Community Jul 29 '22 at 22:59