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:
- Create a Pub/Sub topic to set up as your Cloud Scheduler job target.
- Create a cron job using Cloud Scheduler, and configure a recurring schedule for the job.
- Run your job.
- 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 :
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.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Cloud Scheduler, Pub/Sub APIs.
-
Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access .
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- In the Select a role list, select a role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save .
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project .
-
Enable the Cloud Scheduler, Pub/Sub APIs.
-
Make sure that you have the following role or roles on the project: Cloud Scheduler Admin, Pub/Sub Editor
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access .
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- In the Select a role list, select a role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save .
-
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.
-
In the Google Cloud console, go to the Pub/Sub page.
-
Click Create topic.
-
In the Topic IDfield, enter cron-topicas the ID for your topic.
-
Retain the defaults including the Add a default subscriptionoption.
-
Don't select any other options.
-
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.
-
In the Google Cloud console, go to the Cloud Schedulerpage.
-
Click Create job.
-
Give your job a name.
-
In the Regionlist, select a region for your job.
-
Specify a Frequencyfor your job, using a format based on unix-cron. For example:
30 16 * * 7
For more information, see Configure cron job schedules .
-
In the Timezonelist, 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 typelist, select Pub/Sub.
-
Select the topic you created earlier:
cron-topic
. -
In the Message bodyfield, 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 Schedulerpage.
Your job should be listed.
-
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.
-
In the Status of last executioncolumn, 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.
-
In the Google Cloud console, go to the Pub/Sub subscriptionspage.
-
Click the subscription for your Pub/Sub topic.
-
Click the Messagestab.
-
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.
-
Delete the cron job you created:
-
In the Google Cloud console, go to the Cloud Schedulerpage.
-
Select the checkbox for your job.
-
Click Deleteand confirm the deletion.
-
-
Delete the Pub/Sub topic:
-
In the Google Cloud console, go to the Pub/Subpage.
-
Select the checkbox for your topic.
-
Click Deleteand confirm the deletion.
-
-
Delete the Pub/Sub subscription:
-
In the Google Cloud console, go to the Pub/Subpage.
-
Select the checkbox for your subscription.
-
Click Deleteand confirm the deletion.
-
-
Alternatively, if you created a new project to learn about Cloud Scheduler and you no longer need the project, 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