Schedule and run a cron job by using Cloud Scheduler

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 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 have already done some of these steps.

Install and configure the gcloud CLI:

  1. Install and initialize the gcloud CLI

  2. Update all components:

    gcloud components update
    

Create a project

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

  1. Open a terminal on the machine where you installed the gcloud CLI and create a Cloud 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.

Use the Google Cloud console to enable features:

  1. Enable billing for your project

  2. Enable the Cloud Scheduler API

  3. Enable the Pub/Sub API

Use gcloud to set up Pub/Sub:

  1. Set up a 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 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. If you have no current jobs, click Schedule a job.

  3. Click the Create a job button.

  4. Give your job a name.

  5. Select a region for your job.

    Select a region from the region field.

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

    * * * * *
    

    For more information, see Configuring Cron Job Schedules. Developers familiar with App Engine Cron format can also use that.

  7. Select your timezone.

  8. Click Continue.

  9. In the Target type field, select Pub/Sub from the dropdown menu.

  10. Select the topic you created earlier (cron-topic) from the topic dropdown menu.

  11. Add a string to be sent to your Pub/Sub target in the Message body text box.

  12. Click Create.

You now have a job that sends a message to your 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.

    The results of running a job in the Result column.

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

Verify the results in Pub/Sub

To verify that your 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 Google Cloud account for the resources used on this page, follow these steps.

Delete the Cloud Scheduler job

  1. Go to the Cloud Scheduler page in the Google Cloud 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 Google Cloud 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 Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next