Schedule and run a cron job using the Google Cloud console

This quickstart shows you how to use the Google Cloud console to perform some basic operations using Cloud Scheduler.

In this quickstart, you:

  1. Create a Pub/Sub topic to set up as your Cloud Scheduler job target.
  2. Create a cron job using Cloud Scheduler, and configure a recurring schedule for the job.
  3. Run your job.
  4. Verify that the job has run successfully.

Cloud Scheduler has a free tier and running this quickstart shouldn't incur any costs. For more information, see Pricing.


To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:

Guide me


Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Scheduler, Pub/Sub APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Scheduler, Pub/Sub APIs.

    Enable the APIs

Create a Pub/Sub topic and subscription

A Pub/Sub topic is a resource to which publishers can send messages. To receive messages published to a topic, you must create a subscription to that topic.

  1. In the Google Cloud console, go to the Pub/Sub page.

    Go to Pub/Sub

  2. Click Create topic.

  3. In the Topic ID field, enter cron-topic as the ID for your topic.

  4. Retain the defaults including the Add a default subscription option.

  5. Don't select any other options.

  6. Click Create.

This creates a Pub/Sub topic called cron-topic and a subscription for that topic called cron-topic-sub.

Create a cron job using Cloud Scheduler

Set up a scheduled unit of work known as a cron job that is sent to a target on a recurring schedule.

  1. In the Google Cloud console, go to the Cloud Scheduler page.

    Go to Cloud Scheduler

  2. Click Create job.

  3. Give your job a name.

  4. In the Region list, select a region for your job.

  5. Specify a Frequency for your job, using a format based on unix-cron. For example:

    30 16 * * 7
    

    For more information, see Configure cron job schedules.

  6. In the Timezone list, select a time zone.

    For some time zones, daylight savings time can cause jobs to run or not run unexpectedly. For more information, see Time zone.

  7. Click Continue.

  8. In the Target type list, select Pub/Sub.

  9. Select the topic you created earlier: cron-topic.

  10. In the Message body field, enter a string to be sent to your Pub/Sub target topic. For example: "Hello world!"

  11. Click Create.

You've created a job that sends a message to a Pub/Sub topic at 16:30 on Sundays. You can now run the job.

Run your job

In addition to executing according to its specified schedule, you can force your job to run immediately.

  1. In the Google Cloud console, go to the Cloud Scheduler page.

    Go to Cloud Scheduler

    Your job should be listed.

  2. In the row for your job, click Actions > Force run.

    Note that due to some initial configuration, the first job created in a project can take a few minutes to run.

  3. In the Status of last execution column, a Success status indicates that you have successfully run your cron job.

Next, you can verify that Pub/Sub received the message.

Verify the results in Pub/Sub

Verify that your Pub/Sub topic is receiving messages from your job by pulling Pub/Sub messages from the subscription.

  1. In the Google Cloud console, go to the Pub/Sub subscriptions page.

    Go to Pub/Sub

  2. Click the subscription for your Pub/Sub topic.

  3. Click the Messages tab.

  4. Click Pull.

You should see the message that you published, with the message body, Hello world, and the time when the message was published.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. Delete the cron job you created:

    1. In the Google Cloud console, go to the Cloud Scheduler page.

      Go to Cloud Scheduler

    2. Select the checkbox for your job.

    3. Click Delete and confirm the deletion.

  2. Delete the Pub/Sub topic:

    1. In the Google Cloud console, go to the Pub/Sub page.

      Go to Pub/Sub

    2. Select the checkbox for your topic.

    3. Click Delete and confirm the deletion.

  3. Delete the Pub/Sub subscription:

    1. In the Google Cloud console, go to the Pub/Sub page.

      Go to Pub/Sub

    2. Select the checkbox for your subscription.

    3. Click Delete and confirm the deletion.

  4. Alternatively, if you created a new project to learn about Cloud Scheduler and you no longer need the project, delete the project:

    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