In my python program, I would like it to run a piece of code at a pre-defined time every weekday, let say 2pm Mon - Fri.
How may I do it please?
In my python program, I would like it to run a piece of code at a pre-defined time every weekday, let say 2pm Mon - Fri.
How may I do it please?
You can use "schedule" library
to install, on terminal enter:
pip install schedule
here is an example of the code you want:
#!/usr/bin/python
import schedule
import time
def job():
print("I am doing this job!")
schedule.every().monday.at("14:00").do(job)
schedule.every().tuesday.at("14:00").do(job)
schedule.every().wednesday.at("14:00").do(job)
schedule.every().thursday.at("14:00").do(job)
schedule.every().friday.at("14:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
or you can read the documents to see the other functions Click Here
good luck!
you can make use of crontab linux utility, Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
for your question , goto the python file's directory and enter in terminal
crontab -e
then within crontab file you can enter like this , for eecuting at 2.30pm daily
30 14 * * * python3 your_python_file.py
© 2022 - 2024 — McMap. All rights reserved.
crontab
(assuming you are on linux) orTaskschd.msc
on windows? – Oystercatcheros
andsys
packages to invoke the scheduling tool provided by your OS. – Colchicine