Schedule and run a cron job using the gcloud CLI
This quickstart shows you how to use the gcloud CLI 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.
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.
 - 
      
Install the Google Cloud CLI.
 - 
          
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
 - 
        
To initialize the gcloud CLI, run the following command:
gcloud init - 
  
  
    
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
 - 
      Create a project: To create a project, you need the Project Creator
      (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles. 
- 
        
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. - 
        
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name. 
 - 
  
    
Verify that billing is enabled for your Google Cloud project.
 - 
  
  
    
      
Enable the Cloud Scheduler, Pub/Sub APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable cloudscheduler.googleapis.com
pubsub.googleapis.com  - 
      
Install the Google Cloud CLI.
 - 
          
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
 - 
        
To initialize the gcloud CLI, run the following command:
gcloud init - 
  
  
    
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
 - 
      Create a project: To create a project, you need the Project Creator
      (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles. 
- 
        
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. - 
        
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name. 
 - 
  
    
Verify that billing is enabled for your Google Cloud project.
 - 
  
  
    
      
Enable the Cloud Scheduler, Pub/Sub APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.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 publishers can send messages. 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-topicThis creates a topic called
cron-topic.To receive messages and 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
Use the gcloud scheduler jobs create pubsub
command to set up a unit of work known as a cron job that is sent to a
Pub/Sub target on a recurring schedule. The schedule is specified
using a format based on unix-cron. For more information, see
Cron job format and time zone.
gcloud scheduler jobs create pubsub my-cron-job \
    --schedule="30 16 * * 7" \
    --topic=cron-topic \
    --location="us-central1" \
    --message-body="Hello world"
You've created a job that sends a "Hello world" message to your 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:
gcloud scheduler jobs run my-cron-job --location="us-central1"
Note that due to some initial configuration, the first job created in a project can take a few minutes to run.
Next, you can verify that your Pub/Sub topic received the message.
Verify the results in Pub/Sub
Verify that your Pub/Sub topic is receiving messages from your job.
Pull Pub/Sub messages from a subscription:
gcloud pubsub subscriptions pull cron-sub --limit 5If 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, delete the Google Cloud project with the resources.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Alternatively, delete the resources you created for this quickstart:
Delete the cron job. In Cloud Shell or on the machine where you installed the gcloud CLI, run the command:
gcloud scheduler jobs delete MY_JOB \ --location="LOCATION"Replace the following:
MY_JOB: the name of the job to be deleted.LOCATION: the location of the job. By default, uses the location of the current project's App Engine app if there is an associated app.
Delete the Pub/Sub topic. In Cloud Shell or on the machine where you installed the gcloud CLI, run the command:
gcloud pubsub topics delete TOPIC_IDReplace
TOPIC_IDwith the ID of the Pub/Sub topic to be deleted.Delete the Pub/Sub subscription. In Cloud Shell or on the machine where you installed the gcloud CLI, run the command:
gcloud pubsub subscriptions delete SUBSCRIPTION_IDReplace
SUBSCRIPTION_IDwith the ID of the Pub/Sub subscription to be deleted.
What's next
Learn more about Cloud Scheduler
Learn how to view Cloud Scheduler logs