Now, perhaps you thought to yourself, that’s very nice but I need my script (not necessarily a web app, by the way) to run twice a week to load this new data at work, or to send an email to these customers every night at 3.00 a.m European time, or perhaps as a hobby, you’d like to get in your emailbox that football data freshly scraped overnight?
If so, then keep reading!
While there are many platforms allowing you to do this starting at 0€/month (AWS, Google, Python Anywhere, cron, for example), we will stick with Heroku in this tutorial, to be consistent with my previous article.
This article assumes you have:
- basic knowledge of Linux command line
- read my previous article on pushing Python code on Heroku (recommended)
- the Heroku CLI installed
- basic Git notions
- a Heroku account (free) (do not forget to configure your email address)
- basic Python notions
Plan for today:
Prepare your Python script
In this tutorial, I need a script in which the result is visible so that you can actually view the output, so I decided on … wait for it… sending an email with a scheduled agenda, as this will provide a very visual way to check the results and their timing.
So here I went, creating the folder and starting to populate it with the necessary files:
Then scripting this snippet:
Now adding the
token.json so that the script can access the Gmail account. (Need a refresher? => See here):
As a reminder:
- use a toy email account if this is for hobby
- don’t push such sensitive files as
credentials.jsonto public repos. (By the way, there is this great startup, Gitguardian, which job is to detect such risks and alert accordingly)
Push it on Heroku
Now you must be accustomed to it, so here I go:
Notice I did not specify any app name (heroku create), so Heroku will find an available name randomly. This is because it doesn’t really matter as this is not a website, but only a scheduled process running in the background, so noone will see its name except me (and potential collaborators).
What a great name though! : Enigmatic escarpment!
What happens if I click on the URL provided with the project?
Oouups! Did I mess things up?
No! That’s normal, Heroku provides me with a URL but I’m not making a web app this time.
Trigger a Scheduler
Now that our script is in the cloud, nothing will happen until we select and trigger a scheduler. We will do it the GUI way.
To do so, go to your app page, click on the Resources tab, and start typing ‘schedule’ in the add-ons bar and you will see several schedulers appear:
Depending on your goal and your budget, you may pick one of the above.
I’ll go with the free plan of the advanced scheduler for this tutorial.
Just select the one that best suits your need, and click on submit order form.
Then click on the scheduler to start setting it, and you will see a purple button ‘Create first trigger’, click on it.
At this stage, you might get a pop-up requiring you to fill in your bank details before being able to proceed. If you then opt for a free plan, you can do so without worry of being charged.
You will be prompted to type a name for the trigger, the command to run.
The command is the one you would type in your shell to run your Python program (in this project, the command to type is thus:
Then select your timezone and whether this is a one-off or recurring.
In my case and for this tutorial, I selected recurring every minute:
Finally, you can monitor your scheduler add-on on the dedicated page that follows:
Enjoy your automation!
You can now sit and relax, while Python on Heroku is doing all the nightly work for you!
You remember I wrote and pushed a program that sends emails to myself every single minute, with the time in the subject of the email?
Look at the results:
Just imagine the endless possibilities for your future programs!
Beware though, I do not recommend using a free plan for serious projects, as the reliability might not be up to standards and your dyno might run out.
Also, please use this kind of technology responsibly.
That’s all for today, I’m sure you can now try it yourself and create great things.
Where to Go From Here?
- Creating your own scheduled Python task
- try out other schedulers
- try out the Advanced Scheduler Service API