Run a program on a specific time using python
Question:
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:Windowssystem32calc.exe"
mybat = open(r"D:Cyber_securityPythoncal.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_securityPythoncal.bat"])
when I run it it doesn’t show error but the batch file is not running at specific time. where is the wrong?
Answers:
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)
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.
- Documentation: https://rocketry.readthedocs.io/
- Source code: https://github.com/Miksus/rocketry
I already found my answer.
a = "C:Windowssystem32calc.exe"
mybat = open(r"D:Cyber_securityPythoncal.bat", "w+")
mybat.write(a)
mybat.close()
import sched
from datetime import datetime
import time
def action():
import subprocess
subprocess.call([r"D:Cyber_securityPythoncal.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()
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:Windowssystem32calc.exe"
mybat = open(r"D:Cyber_securityPythoncal.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_securityPythoncal.bat"])
when I run it it doesn’t show error but the batch file is not running at specific time. where is the wrong?
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)
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.
- Documentation: https://rocketry.readthedocs.io/
- Source code: https://github.com/Miksus/rocketry
I already found my answer.
a = "C:Windowssystem32calc.exe"
mybat = open(r"D:Cyber_securityPythoncal.bat", "w+")
mybat.write(a)
mybat.close()
import sched
from datetime import datetime
import time
def action():
import subprocess
subprocess.call([r"D:Cyber_securityPythoncal.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()