Schedule and run a cron job
This quickstart shows you how to perform some basic operations using Cloud Scheduler.
In this quickstart, you:
- 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 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:
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- If you are not using Cloud Shell, update the Google Cloud CLI
components:
gcloud components update
- Log in using your account:
gcloud auth login
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.
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
.To view the results of your job, create a Pub/Sub subscription:
gcloud pubsub subscriptions create cron-sub --topic cron-topic
Create a Cloud Scheduler job
Set up a scheduled unit of work known as a cron job that is sent to a target on a recurring schedule.
In the Google Cloud console, go to the Cloud Scheduler page.
Click Create job.
Give your job a name.
In the Region list, select a region for your job.
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.
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.
Click Continue.
In the Target type list, select Pub/Sub.
Select the topic you created earlier:
cron-topic
.In the Message body field, enter a string to be sent to your Pub/Sub target topic. For example: "Hello world!"
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.
In the Google Cloud console, go to the Cloud Scheduler page.
Your job should be listed.
In the row for your job, click > Force a job run.
ActionsNote that due to some initial configuration, the first job created in a project can take a few minutes to run.
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 Cloud Scheduler job.
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.
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.
Delete the Cloud Scheduler job you created:
In the Google Cloud console, go to the Cloud Scheduler page.
Select the checkbox for your job.
Click Delete and confirm the deletion.
Delete the Pub/Sub topic:
In the Google Cloud console, go to the Pub/Sub page.
Select the checkbox for your topic.
Click Delete and confirm the deletion.
Delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
- Learn more about Cloud Scheduler
- Learn how to view Cloud Scheduler logs