Google Cloud Platform's Cloud Scheduler.

If you’re looking to run code regularly, like a cron job, you can use Google Cloud Platform’s Cloud Scheduler to automatically run serverless Cloud Functions at fixed time intervals, without using any actual servers.

What Is Cloud Scheduler?

Cloud Scheduler advertises itself as “Managed Cron as a Service.” It allows you to schedule tasks using cron syntax. However, it only replaces the scheduling component of cron—Cloud Scheduler isn’t a compute service, and it can only send HTTP requests or send pub/sub messages, so on its own it’s fairly limited in scope.

One of the most useful applications of the scheduler is to trigger Cloud Functions at regular intervals. Cloud Functions run code without servers, using various runtimes such as JavaScript with Node, Python, or Java. You can set up Cloud Functions to trigger from Pub/Sub notifications, which Cloud Scheduler can send, so you end up with a system where you can run serverless code regularly using cron syntax.

If the code you need to run needs to happen on a specific server, the best option is still to just use local cron to run any scripts you need. However, if you can script it with JavaScript/Python, and don’t care about the execution environment of the function, Cloud Functions will handle it well without requiring you to set up a server.

Setting Up a Scheduled Function

Head over to the Cloud Functions console to set up a function. You don’t need to make a new function, as you could just edit an existing one to run off of Pub/Sub messages.

Give it a name, choose how much RAM to allocate to it, and switch it over to “Cloud Pub/Sub” as the trigger.

choose pub sub

You’ll need to select or create a Pub/Sub topic for the function to subscribe to.

create new topic

Give it a name, and click “Create Topic.”

give the topic a name

Once that’s done, you can upload your code, either by pasting it inline or uploading a zip, or by linking a Cloud Source repository.

upload code

Head over to the Cloud Scheduler console to create the cron job.

create new cron job

Give it a name, and set the frequency using cron syntax. You can read our guide to cron or use this online tool to help you with the scheduling. The general syntax is:

minute hour day month weekday

Set the target to Pub/Sub, and enter in the topic name you created for the function.

fill out job settings

The function should now run automatically, but you can click “Run Now” from Cloud Scheduler to test it out. You can also view the logs for previous executions from this panel.

run now

If the function isn’t executing properly, check the logs of the Cloud Function to make sure it’s responding to requests at all. If it’s getting requests, it’s probably an error with your code, and if it isn’t, it’s likely an error or mispelling with the Pub/Sub topic.

Profile Photo for Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times.
Read Full Bio »