0
0
GCPcloud~5 mins

Cloud Scheduler for cron jobs in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Cloud Scheduler lets you run tasks automatically at set times, like a clock. It helps you run jobs regularly without needing to remember or do them manually.
When you want to send daily reports automatically at 8 AM.
When you need to clean up old files every week without manual work.
When you want to trigger a backup process every night.
When you want to call a web service every hour to update data.
When you want to run a script every Monday morning to prepare your system.
Config File - cloud-scheduler-job.yaml
cloud-scheduler-job.yaml
apiVersion: cloudscheduler.googleapis.com/v1
kind: Job
metadata:
  name: example-cron-job
  description: "Daily job to call a Pub/Sub topic"
spec:
  schedule: "0 8 * * *"
  timeZone: "America/New_York"
  pubsubTarget:
    topicName: projects/example-project/topics/example-topic
    data: "SGVsbG8sIGNsb3VkIHNjaGVkdWxlciE="

This file defines a Cloud Scheduler job named example-cron-job. It runs every day at 8 AM New York time. The job sends a message to a Pub/Sub topic called example-topic. The data field is the message content encoded in base64.

Commands
This command creates a Cloud Scheduler job named 'example-cron-job' that runs daily at 8 AM New York time. It sends the message 'Hello, cloud scheduler!' to the Pub/Sub topic 'example-topic'.
Terminal
gcloud scheduler jobs create pubsub example-cron-job --schedule="0 8 * * *" --time-zone="America/New_York" --topic=example-topic --message-body="Hello, cloud scheduler!"
Expected OutputExpected
Created job [projects/example-project/locations/us-central1/jobs/example-cron-job].
--schedule - Defines the cron timing for the job.
--time-zone - Sets the time zone for the schedule.
--topic - Specifies the Pub/Sub topic to send the message to.
This command lists all Cloud Scheduler jobs in your project to verify the job was created.
Terminal
gcloud scheduler jobs list
Expected OutputExpected
NAME SCHEDULE TIME_ZONE STATE example-cron-job 0 8 * * * America/New_York ENABLED
This command manually triggers the 'example-cron-job' to run immediately for testing purposes.
Terminal
gcloud scheduler jobs run example-cron-job
Expected OutputExpected
Job [example-cron-job] triggered.
This command shows detailed information about the 'example-cron-job', including schedule, target, and status.
Terminal
gcloud scheduler jobs describe example-cron-job
Expected OutputExpected
name: projects/example-project/locations/us-central1/jobs/example-cron-job schedule: '0 8 * * *' timeZone: America/New_York pubsubTarget: topicName: projects/example-project/topics/example-topic data: SGVsbG8sIGNsb3VkIHNjaGVkdWxlciE= state: ENABLED
Key Concept

If you remember nothing else from this pattern, remember: Cloud Scheduler runs tasks automatically on a set schedule without manual effort.

Common Mistakes
Using incorrect cron syntax in the schedule flag.
The job will not run at the expected times or may fail to create.
Use a valid cron expression like '0 8 * * *' for daily at 8 AM.
Not specifying the correct time zone.
The job runs at unexpected times because it defaults to UTC.
Always set --time-zone to your local or desired time zone.
Using a Pub/Sub topic that does not exist.
The job creation fails or the job cannot send messages.
Create the Pub/Sub topic before creating the scheduler job.
Summary
Create a Cloud Scheduler job with gcloud to run tasks on a schedule.
List jobs to verify creation and check their status.
Manually run jobs to test them immediately.
Describe jobs to see detailed configuration and state.