Schedule and run a cron job

This quickstart shows you how to perform some basic operations using Cloud Scheduler.

In this quickstart, you:

  1. Create a cron job using Cloud Scheduler.
  2. Set a recurring schedule for a job.
  3. Specify a Pub/Sub topic as the job target.
  4. Run a job.
  5. Verify that the job has run successfully.

Cloud Scheduler has a free tier and running this quickstart should not result in 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. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. If you are not using Cloud Shell, update the Google Cloud CLI components:
    gcloud components update
  11. Log in using your account:
    gcloud auth login
  12. Enable the Cloud Scheduler and Pub/Sub APIs:

    gcloud services enable cloudscheduler.googleapis.com pubsub.googleapis.com

Create a Pub/Sub topic and subscription

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

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

    gcloud pubsub topics create cron-topic
    

    This creates a topic called cron-topic.

  2. To view the results of your job, create a Pub/Sub subscription:

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

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 a job 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.

  1. Pull Pub/Sub messages from a subscription:

    gcloud pubsub subscriptions pull cron-sub --limit 5
    

    If there are no messages pulled initially, run the command again.

  2. View the results of running your job. The output should look similar to the following:

    DATA: Hello world!
    MESSAGE_ID: 5028933846601543
    ORDERING_KEY:
    ATTRIBUTES:
    DELIVERY_ATTEMPT:
    ACK_ID: RFAGFixdRkhRNxkIaFEOT14jPzUgKEUQAgVPAihdeTFXLkFacGhRDRlyfWB9[...]
    

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 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