Keep free Heroku app awake for free using Google App Script in 2020

Cheska the Panda
3 min readMar 13, 2017

--

Heroku allows you to host your app for free. This is great for hosting your side project for demo purpose. However, if you are with a free plan, which means you are using free dynos, and according to the official documentation from Heroku, currently free dynos will go to sleep after 30 minutes of inactivity. Depending on your purpose of hosting the app, this might not be something you want (e.g. you don’t want to wait extra time to wake up your app during your demo, which might make your app look slow).

After some Google search, I come across several solutions. For example, in this article, the author has suggested several ways to keep heroku app awake. However, some of them are not applicable anymore today (either they no longer work anymore, or no longer free). One thing we need to note before proceed is that you can no longer keep your free heroku app awake for 24hrs, they are now (as of Mar. 2017)only allowed 18 hours awake per 24 hour period, see here.

Since I don’t want to modify my source code (e.g to use setInterval on my backend, or use services such as cloudflare to cache my data), I decided to go with Google App Script. With Google App Script, I can simply write some scripts to visit my heroku app at the time I want. And it is very simple to implement as well.

  1. Create a new Google Sheet document, go to Tools -> Script Editor
  2. Enter 1 at cell B1, which will work as a counter.
  3. Enter the following code:
function myFunction() {
var d = new Date();
var hours = d.getHours();
var currentTime = d.toLocaleDateString();
var counter = SpreadsheetApp.getActiveSheet().getRange(‘B1’).getValues();

if (hours >= 6 && hours <= 18) {
var response = UrlFetchApp.fetch(“your_heroku_app_address");
SpreadsheetApp.getActiveSheet().getRange(‘A’ + counter).setValue(‘Visted at ‘ + currentTime + “ “ + hours + “h”);
SpreadsheetApp.getActiveSheet().getRange(‘B1’).setValue(Number(counter) + 1);
}
}

Edit: If you copy and paste this code, and run it in the script editor, you may encounter some illegal characters error. You need to replace all quotation marks and/or apostrophes with your own to solve this problem (thanks to Andrew Min for pointing it out).

What this script does is to check if the current time is between 6am to 6pm. If yes, it will visit my heroku app, and then log this activity on the accompanying Google Sheet at Column A.

In the accompanying Google Sheet, I used the cell B1 as a counter to record the number of visits as well as an indication of the row number to record the activity.

You can customize the logic based on your own needs, for example, you can ask the script only to run on week days, and to log the exact time of its visit if you want (my script only logs the hour).

4. Save your scripts. And inside the Script Editor, go to Edit -> current project’s trigger.

5. Add a new trigger, select Time-driven events, minutes timer, and select the time interval you want this script to visit your heroku app. And save.

And it’s done. Google will run your scripts based on your settings, and its activity will be logged on the accompanying Google Sheet for your reference.

By the way, Heroku offers a hobby plan at a pretty reasonable cost ($7/month). And with the hobby plan your app will not go to sleep. You may want to check it out and see if it fits your needs better.

--

--