This post will demonstrate how to use Cron to set up a daily schedule for receiving the weather forecast email.
The first step will be to go to crontab.guru to check the format. I want to have this email set to go out every day at 8 AM. The Cron format will be: 0 8 * * *
Now we go back to the CLI to access Cron with crontab -e
To set up the Cron job, we need to make a new line at the bottom that includes:
– The time format (0 8 * * *)
– A pointer to the Python interpreter (/usr/bin/python3)
– The pathway to our script file (/home/kali/weather_email/daily_weather.py)
To test the job, I will set the Cron time to be 1 minute beyond whatever the current time is at the moment. For example, if the time is 11 AM, I will use 01 11 * * * for the job to run at 1101 AM.
If everything is set up correctly, I will receive an email on the account I specified in the code…
And, no email.
In the process of trying to figure out why the job was not running, I decided to add a line to the job that will write a log file with errors:
>> /home/kali/weather_email/cron.log 2>&1
After the job attempted to run again, I get this error report:
Looking into this error I realized that Cron needs a direct file path to the weather_template.html
file. Otherwise, it doesn’t know where to find the file. To fix this, we need to go back into the script and add the full pathway to the file.
Now the Cron job can run, and the email is received.