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 =
if now.hour==9 and now.minute==30:
print(“Its time to run the code”)
# Call your CODE() function here

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

Hey hello!!
Try using the below code

import datetime
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 -


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():


    now_time ='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('Current Time :',IST)
            print('Set Time :',settime)

while True:

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 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
while True:
____millis = int(time.time()*1000)
____if millis==mytime:
________print(“My code is running”)

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 will execute on 12:30 AM on every Sunday.
30 12 0 * *

1 Like

@armsvs Very important and simplest one.

import schedule //install schedule module first
import time

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


while True:

code end

here for another time u can use following:

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

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