How to run python code at a specific time of the day?

Hi, I would like to run a python code at a particular time of the day. How can it be done?

from datetime import datetime

//write your code which you need to run here… and define a eg.CODE() function for ur entire code, then follow further code

while True:
now = datetime.now()
if now.hour==9 and now.minute==30:
print(“Its time to run the code”)
# Call your CODE() function here
break

Showing invalid syntax for this
if now.hour==9 and now.minute==30:

Hey hello!!
Try using the below code

import datetime
t=datetime.datetime.now()
print(t.hour, t.minute, t.second)

This will return the current time.
So that you can pass this into a while loop i.e., the condition of while loop is
t==3:10:20// time can be preferred by you as hour:minute:second format.

Hope this would help you.

Is all indentations are correct?
ensure TAB before if, then double tab before print and double tab before break
check once

Hi @armsvs,

You can run your python script as cronjob. Check the below links -

  1. https://ostechnix.com/a-beginners-guide-to-cron-jobs/
  2. https://medium.com/@gavinwiener/how-to-schedule-a-python-script-cron-job-dea6cbf69f4e

Do let me know in case you need any other information.

Hi @Devendra, @janakiguduru444

With reference codes provided by you, I have made the program as below. But now the issue is how to add a delay of seconds in this program. time.sleep function is not supporting here. Please help!!

Thanks in advance

from datetime import datetime
from pytz import timezone
from datetime import time

def time_call():

    f='%H:%M:%S'

    now_time = datetime.now(timezone('Asia/Kolkata'))

    IST = now_time.strftime(f)
    ist = str(IST)

    settime = time(hour =13 , minute =28, second = 5)
    stime = str(settime)

    if ist == stime:
            print('y')
            print('Current Time :',IST)
            print('Set Time :',settime)

while True:
time_call()

hey @armsvs
It’s a quit long procedure but can be help.
I think your program is running 100 times when it acquire a time condition ri8…
you need to give him a condition in milliseconds.
So, how it would be done… just simply visit the website https://currentmillis.com/ and there u need to convert ur required time in milliseconds.

Enter todays date first in given format… Then enter the time on which time u required to run the code… for ex. I entered 2020/07/31 and time 02:50:00 (Note that: here time is in 24 hr format) … then I will get a milisecond below are 1596144000000. Just copy the result.

then here is python code:

import time
mytime= 1596144000000
perDay=86400000
perHour=3600000
perMinute=60000
perSec=1000
while True:
____millis = int(time.time()*1000)
____if millis==mytime:
________print(“My code is running”)
________mytime=mytime+perDay

change perDay with perHour, perMinute, perSec for different interval
these are the values of miliseconds increase with respect to Day, Hour, Minute or second.

Tell me if it works then.

Use corn service in linux. you can schedule python script in cron for specif time on specific day.
There are 5 field in cron. MIN HOUR DOM(day of month) MON(month) DOW(day of week)
crontab -e to edit/add new cron
for eg,
Below python script.py will execute on 12:30 AM on every Sunday.
30 12 0 * * script.py

1 Like

@armsvs Very important and simplest one.
code:

import schedule //install schedule module first
import time

def RunMyCode():
____print(“Here I am”)

schedule.every(4).seconds.do(RunMyCode)

while True:
____schedule.run_pending()
____time.sleep(1)

code end

here for another time u can use following:
schedule.every(10).minutes.do(RunMyCode)
schedule.every().hour.do(RunMyCode)
schedule.every().day.at(“10:30”).do(RunMyCode)
schedule.every(5).to(10).minutes.do(RunMyCode)
schedule.every().monday.do(RunMyCode)
schedule.every().wednesday.at(“13:15”)do(RunMyCode)
schedule.every().minutes.at(":17").do(RunMyCode)

Try using a while loop where the condition within the loop is the particular time at which you wish it to be executed.

import datetime
t=datetime.datetime.now()

while(t==/prefered time/)
{
…executable part…
}

This would help to execute at a particular time.

you can connect it to a open source site that gives time, maybe google should do it.
and then call your python program at any specific time you desire