Cloud Scheduler Quickstart

This quickstart walks you through performing the following basic operations using Cloud Scheduler:

  • Create a Cloud Scheduler job.
  • Set a recurring schedule for a job.
  • Specify a Cloud Pub/Sub topic as the job target.
  • Run a job.
  • Verify success.

Cloud Scheduler has a free tier, so running this quickstart should not result in any costs. For more information, see Pricing.

Before you begin

Use the following instructions to prepare for this quickstart. You may already have already done some of these steps.

Install and configure the gcloud command line tool:

  1. Install and initialize the Cloud SDK

  2. Add the gcloud components for beta products:

     gcloud components install beta
    
  3. Update all components:

     gcloud components update
    

Create a project with an App Engine app.

Or you can use an existing project, if you have one.

  1. Open a terminal on the machine where you installed the Cloud SDK and create a GCP project:

     gcloud projects create [PROJECT_ID]
    

    where PROJECT_ID is the ID for the project you want to create. Project IDs must start with a lowercase letter and can have lowercase ASCII letters, digits or hyphens. They must be between 6 and 30 characters.

  2. Configure gcloud to use the project you have chosen:

     gcloud config set project [PROJECT-ID]
    

    where PROJECT_ID is the ID you set previously.

  3. Now create the app:

     gcloud app create [--region=REGION]
    

    where [REGION] is one of the supported locations. Currently the supported regions include:

    • asia-northwest1
    • europe-west1(use europe-west for App Engine commands)
    • us-central1(use us-central for App Engine commands)
    If you are using an existing project that has an App Engine app, it must use one of these locations. You can check the region with the command:

     gcloud app describe
    

    The region is listed by locationId, for example, locationId: us-central. If the region isn't a supported region, you cannot use this project because an App Engine region can't be changed. Once you set a zone for the App Engine app, you cannot change it.

Use the Google Cloud Platform Console to enable features:

  1. Enable billing for your project

  2. Enable the Cloud Scheduler API

  3. Enable the Cloud Pub/Sub API

Use gcloud to set up Cloud Pub/Sub:

  1. Set up a Cloud Pub/Sub topic to use as a target for your cron job:

    gcloud pubsub topics create cron-topic
    

    This command creates a topic called 'cron-topic'. Make a note of the name because you use this later in the quickstart.

  2. Create a Cloud Pub/Sub subscription; you need this to view the results of your job.

    gcloud pubsub subscriptions create cron-sub --topic cron-topic
    

Create a job

  1. Visit the Cloud Scheduler page in the console:

    Cloud Scheduler

  2. Click the Create job button.

  3. Give your job a name and optionally add a description.

    image

  4. Specify the frequency for your job, using the unix-cron format.

    * * * * *
    

    See Configuring Cron Job Schedules for more information.

  5. Select your timezone.

  6. In the Target field, select Pub/Sub topic from the dropdown menu and enter the topic you created earlier (cron-topic).

  7. Add a Payload string to be sent to your Cloud Pub/Sub target.

    image

  8. Click Create.

You now have a job that sends a message to your Cloud Pub/Sub topic every minute. Let's run the job you just created.

Run your job

  1. In the Cloud Scheduler console page opened with your project, click the Run now button.

    The first job created in a project can take a few minutes to run the first time it is invoked, because of required configuration. So you might need to wait a bit.

  2. View the results under the Result column.

    image

Congratulations! You have just viewed the results in the console of running a cron job that sends a message to Cloud Pub/Sub. Next, you'll learn how to verify that Cloud Pub/Sub actually got the message.

Verify the results in Cloud Pub/Sub

To verify that your Cloud Pub/Sub topic is receiving messages from your job:

  1. Invoke the following command:

     gcloud pubsub subscriptions pull cron-sub --limit 5
    
  2. View the results. You should see output that looks roughly like the following

     +-------------+----------------+------------+-----------------------------+
     | DATA        | MESSAGE_ID     | ATTRIBUTES | ACK_ID                      |
     +-------------+----------------+------------+-----------------------------+
     | hello cron! | 56344662920621 |            | EkwnGERJUytDCypYEU4EISE-... |
     +-------------+----------------+------------+-----------------------------+
     | hello cron! | 56340561244339 |            | EkwnGERJUytDCypYEU4EISE-... |
     +-------------+----------------+------------+-----------------------------+
     | hello cron! | 56340388879975 |            | EkwnGERJUytDCypYEU4EISE-... |
     +-------------+----------------+------------+-----------------------------+
     | hello cron! | 56337745052761 |            | EkwnGERJUytDCypYEU4EISE...  |
     +-------------+----------------+------------+-----------------------------+
     | hello cron! | 56337890896134 |            | EkwnGERJUytDCypYEU4EISE-... |
     +-------------+----------------+------------+-----------------------------+
    

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

Delete the Cloud Scheduler job

  1. Go to the Cloud Scheduler page in the GCP Console.

    Go to the Scheduler page

  2. Click the checkbox next to your job.

  3. Click the Delete button at the top of the page and confirm your delete.

Delete the Pub/Sub topic

  1. Go to the Cloud Pub/Sub page in the GCP Console.

    Go to the Pub/Sub page

  2. Click the checkbox next to your topic.

  3. Click Delete at the top of the page and confirm your delete.

Delete the project

If you created a project just for this quickstart.

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Scheduler