Search code examples
pythoncroncron-task

cronjob not runnig after hours are specified?


Hey I was testing cronjobs for one of my projects, for that I made a simple telegram bot send me a msg every minute

the code is

import requests
from datetime import datetime
Token = "Token here"
OWNER_ID = Id here


def send():

    send_text="Message sent at : "+str(datetime.now())
    telegram_send = f"https://api.telegram.org/bot{Token}/sendmessage?chat_id={OWNER_ID}&text={send_text}"
    requests.get(telegram_send)
    print("sent at : " , datetime.now())


send()

and my cronjob command was

* * * * * /usr/bin/python3 /home/anish/cowin/tester.py >> ~/cron.log 2>&1

the bot sent me a msg every minute and the log files also recorded it

sent at :  2021-05-26 14:37:02.681277
sent at :  2021-05-26 14:38:02.444642
sent at :  2021-05-26 14:39:02.189234
sent at :  2021-05-26 14:40:01.932228
sent at :  2021-05-26 14:41:02.695751

and this is what I received in the bot as expected Image from bot

But when I changed the cronjob command to execute the command between 2pm and 6pm the program execution stopped

the new command I enterd is

* 14-18 * * *  /usr/bin/python3 /home/anish/cowin/tester.py >> ~/cron.log 2>&1

can some one help me out with this problem or point out any error in my code??


Solution

  • I found out that the cron services didn't detected the change in my systems timezone .

    I got the issue solved by simply restarting cron services sudo /etc/init.d/cron restart