Hacky tip: Use Google Sheets to run scheduled/recurring Cronjobs

As I mentioned before Google apps scripts is quite powerful and you can do some really cool stuff with it and boost your productivity.

It has some greater trigger functionality, which you can use to run functions on time scheduled or recurring basis. We will use this functionality to make web requests and basically acts as our Cronjob manager.

NOTE: This is a bit of a hack and not recommended for time critical production jobs. Google might probably block you from abusing it, if you overuse this feature.

  1. To start create a new or open an existing Google Sheet:
  2. Go to Tools -> Script Editor
Open the Script Editor

3. In the new function, lets just leave it as myFunction for demo purposes, add the following code snippet:

function myFunction() {
var url = 'http://yourdomain.com/cronjob';
var options = {
'method': 'get'
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}

So what we do here is do a web request to a URL. Please add your own URL. You can change the method to POST or anything else if necessary. The UrlFetchApp is the built-in web request library and then we log the response using the Logger library.

So before we schedule the job, first test by clicking on the Run or Debug button to see if it’s working as expected.

Run or debug your function

If you’re happy with the outcome the next step is to schedule this job.

4. Click on the Current Project Trigger’s button

Schedule your job by clicking on the current project triggers button

You can add a new trigger:

Add a new trigger

You should leave the trigger as time-driven and then you can choose the interval or the date and time if it will be once off job.

Below the trigger is also a notifications link. You can setup email notifications to be sent to you if the job failed.

So it is very simple, but also super powerful. As mentioned above, I repeat: This is a bit of a hack and not recommended for time critical production jobs. Google might probably block you from abusing it, if you overuse this feature.

Give me a shout if you’re running into any problems or you need assistance.

--

--