How to use CRON on Heroku for free

Marc-Etienne Dartus
4 min readJun 1, 2020

--

Photo by Djim Loic on Unsplash

You might want to create some CRON jobs on Heroku? I will tell you why and how it’s possible. I’m creating this article to help you achieve your goal since I was facing a lot of issue with it and I was hard to find a good solution.

What is the result?

The goal is to have a CRON system inside our application to execute some action. You can use every language you want since the most important thing to understand is how it works.

Here is an example in JavaScript:

const schedule = require('node-schedule');schedule.scheduleJob('*/15 * * * *', ()=>{
// Execute something every 15 minutes
});

How is it possible?

But free dyno shuts down after 30 minutes of inactivity?

I understand that you want to use your scheduled action at any time an not only 30 after your last use, so here is the trick. We will ping periodically the app to stay up all the time!

But it will cost me money to stay up all the time?

Not at all, you can have 1000 hours of free dyno use a month!

“Personal accounts are given a base of 550 free dyno hours each month. In addition to these base hours, accounts which verify with a credit card will receive an additional 450 hours added to the monthly free dyno quota. This means you can receive a total of 1000 free dyno hours per month, if you verify your account with a credit card.” — https://devcenter.heroku.com/articles/free-dyno-hours

Quick math: 31 days x 24 hours = 744 hours.

744 is way less than 1000 hours, so we can have a free dyno that runs all the time for free to do what we want and still have other apps that use the remainder!

How to achieve this?

I. Create an account

Create your free account on Heroku and add your billing information. Don’t worry you won't be charged if you use more than 1000 hours and you will get a warning of your quota.

“When you exceed 80% of your free dyno quota, you will receive an email notification warning you of your account usage, giving you time to adjust free dyno usage across your apps.

A second notification will be sent when you reach 100% of your account quota, at which point your application’s dynos will be put to sleep for the remainder of that month. As a result, any apps using free dynos will not be accessible for the remainder of the month.” — https://devcenter.heroku.com/articles/free-dyno-hours

II. Create the scheduling file

Now create a file with the scheduling script. For example: Schedule.js

const schedule = require('node-schedule');schedule.scheduleJob('*/15 * * * *', ()=>{
// Execute something every 15 minutes
});

III. Create the Procfile

The Procfile and the clock variable is maybe the most important thing to remember. Heroku don’t have a clock process if you don’t tell him that you need it! If you want more information about it you can see the documentation.

Now create a file called: Procfile. Inside the file, add “clock=” and how to run the file. Like for node.js we use node filename.js or for python, we use py filename.py.

clock:  node schedule.js

IV. Create the ping system

Now you need to create some folder and a file: /lib/tasks/schedule.rake

desc "Pings PING_URL to keep a dyno alive"
task :dyno_ping do
require "net/http"
if ENV['PING_URL']
uri = URI(ENV['PING_URL'])
Net::HTTP.get_response(uri)
end
end

V. Deploy your project

Now you have to upload your project to Heroku to run it. If you never done that, I recommend you to use the “Deploying with git method” and here is some other way to do it :

VI. Keep your project always online

Add PING_URL to your Heroku environment:

$ heroku config:add PING_URL=http://Procfile.herokuapp.com

Set up Scheduler:

$ heroku addons:add scheduler:standard
$ heroku addons:open scheduler

That last command should open the Scheduler interface in your browser. You can now set up your dyno_ping task to run every 10 minutes:

$ rake dyno_ping

VII. (Optional) Use multiple pinging system

I highly recommend you to use also another way of pinging your application. The system of clock from Heroku can fail as they mentioned in their documentation, so you can use other systems.

Here is two medium article explaining how to use “cron-job” or “Kaffeine”.

You are now officially a hacker since you use the free tier as if it was the paid one!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Marc-Etienne Dartus
Marc-Etienne Dartus

Written by Marc-Etienne Dartus

🚀Software Engineering at @Amazon Madrid. 🤖 Specialized in Data Science. 👨🏻‍💻 https://github.com/medartus. Marketing Newsletter: https://www.foundernotes.io

Responses (3)

Write a response