I have a PHP application that is is on a shared hosting provider. I have no access to cron or anything else to run a scheduled task. I can’t fake cron by relying on a visitor to the application to call a PHP function to run the task. So, I set up a scheduled task on my Windows server and wrote a quick VB script to to call the PHP script. Problem solved? Sure, but only if I wanted to rely on the availability of that particular server and Comcast business internet to always run my scheduled tasks. And, who knows, maybe you don’t have access to a server to do this for you or have a computer that is always on and connected to the internet.
Now, I had heard of Google Apps Script, but I’m not a “real” programmer so I didn’t think much of it until I stumbled across a post the other day that made me investigate the API a little further. It was quickly apparent that I could use Google Apps Script to replace my scheduled tasks. Note in advance, if you need your task to fire at a precise time, this isn’t for you. If, however, you can live with it running within a given hour read on. Here’s an outline to get you started. I’ve linked to the docs I used below.
- Go to Google Docs
- Create New > Spreadsheet
- Go to Tools > Script editor
- A new window will open and you will see a function stubbed out for you
- Rename your function to something meaningful
- Add a method to fetch the URL of the script you wish to run
- Go to Triggers > All your triggers
- You’ll be prompted to name your script
- Now add your triggers to run your script
For example, I have a function that runs a daily production report:
function getProductionReport() {
var response = UrlFetchApp.fetch("http://example.com/reports/production_report.php?token=sometoken")
}
The reason I added a token to the URI is that I have the script check for the correct token before running the report. It won’t run without it and even if it did, no output is returned to the caller.
Next, I created the triggers. When you open the All your triggers window, you’ll be prompted to name your script. Do so. Now, you will be able to add a new trigger. Choose new and under run you’ll see a drop down with your function. Next, change Events to Time-driven. Next choose your timer (Minutes, Hour, Day, Week) then choose the interval you desire. If you only want your task to run on weekdays, you will have to create a separate trigger for each day of the week. That’s pretty lame, but I’m sure Google will improve this as time goes on. Choose save and that’s it, you’re done.
Add as many functions and timers as you like and let Google’s servers do the rest. Oh, and here is the documentation from Google:
Class UrlFetchApp and scroll down here for Time-driven triggers
When I have more time, I’m going to do a much deeper dive here. It’s pretty crazy what you can do with Google Apps Script. If you are using Google Apps Script for something trivial or complex, I’d love to hear about it. Drop me a comment below.
One thing that hit me after I implemented this and saw it running day after day was that, out of everything else Google does every millisecond they always remember to run my little functions. Pretty damn impressive.
