Recently, I was doing an automation tool project and I got to deploy a Python script with cron jobs to run this tool at a specified time on a daily basis.
I ended up utilizing multiple sources to figure out things and deploy the script. Today, I decided to write about it to help other folks in case if they struggle with the same situation. So, let's get started...
Step #0
- Create an account on Heroku.
- Install Heroku CLI. Follow this amazing guide by Heroku to get started with CLI.
Once you have installed CLI, use the command heroku --version
to verify the installation.
Step #1
- After setting up the Heroku CLI on your machine, the next step is to setup the project which uses cron jobs. For this purpose, I have create a small Python project which utilizes Advanced Python Scheduler to set cron jobs.
main.py
: It contains following piece of code:
from datetime import datetime
def cronjob():
"""
The main cronjob function to be run continuously.
"""
print(" Hello folks, Cron job is running")
print("The current time is: %s" % datetime.now())
All it doing is, importing datetime
function from the built-in library and using it to print the current time. We will use this function in our cron job and run it at a specified time using our scheduler.
2.cronjob.py
: It contains following piece of code:
# Package Scheduler.
from apscheduler.schedulers.blocking import BlockingScheduler
# Main cronjob function.
from main import cronjob
# Create an instance of scheduler and add function.
scheduler = BlockingScheduler()
scheduler.add_job(cronjob, "interval", seconds=30)
scheduler.start()
Like main.py
, this is also pretty straight forward. All I'm doing is, importing blocking scheduler from the library, initializing an instance, and then setting the cronjob function to start it.
Step #2
The next step is to create the Procfile
. Procfile tells Heroku what to do. When you deploy your project on Heroku, Procfile says to Heroku, Hey! buddy you gotta run these things!
Now, inside your Procfile, paste the following line of code:
clock: python cronjob.py
It says, Hey Heroku, run this command and it's a clock process. Heroku uses clock processes to run the cron jobs.
Step #3
The final step is to deploy the project to Heroku. Run the following command to deploy the project on Heroku:
git push heroku deploy:master
It will take a couple of seconds to deploy. Once deployed, you will get a message saying:
# remote: Verifying deploy... done.
Wow! Your script has been deployed successfully. The very last step is to spin a clock process for our app.
Now, by default, your Heroku environment does not have a clock process. But since we have specified in our Procfile that we are going to use it, so we have to provision one. To do so, run the following command:
heroku ps:scale clock=1
It will take a couple of seconds and show following message on completion.
# Scaling dynos... done, now running clock at 1:Free
🎯That's all! You have successfully deployed your first script on Heroku for free. Just a couple of steps and you are all set.
Verify It
To do so, run the following command in your project directory to get real-time logs from your deploy script:
heroku logs --tail
Heroku logs might look like this.
Thank you for reading! Do share your valuable feedback and suggestions!