Bird
Raised Fist0
GCPcloud~5 mins

Cloud Scheduler for cron jobs in GCP - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Google Cloud Scheduler?
easy
A. To store large amounts of data
B. To create virtual machines
C. To run tasks automatically at scheduled times
D. To monitor network traffic

Solution

  1. Step 1: Understand Cloud Scheduler's role

    Cloud Scheduler is designed to run jobs automatically based on a schedule you set.
  2. Step 2: Compare options with this role

    The incorrect options describe storing large amounts of data, creating virtual machines, and monitoring network traffic, which are other GCP services.
  3. Final Answer:

    To run tasks automatically at scheduled times -> Option C
  4. Quick Check:

    Cloud Scheduler = automatic task runner [OK]
Hint: Cloud Scheduler runs jobs on a timer, like an alarm clock [OK]
Common Mistakes:
  • Confusing with storage or VM services
  • Thinking it monitors traffic
  • Assuming it manages databases
2. Which of the following is the correct format for a cron schedule in Cloud Scheduler?
easy
A. day month year hour minute
B. year month day hour minute
C. hour minute second day month
D. minute hour day month day-of-week

Solution

  1. Step 1: Recall cron format used by Cloud Scheduler

    Cloud Scheduler uses the standard cron format: minute, hour, day of month, month, day of week.
  2. Step 2: Match options to this format

    minute hour day month day-of-week matches this order exactly; others have incorrect sequences or extra fields.
  3. Final Answer:

    minute hour day month day-of-week -> Option D
  4. Quick Check:

    Cron format = minute hour day month day-of-week [OK]
Hint: Cron starts with minute, then hour, then day [OK]
Common Mistakes:
  • Mixing up order of fields
  • Including year field incorrectly
  • Confusing day and month positions
3. Given this cron expression in Cloud Scheduler: 0 9 * * 1, when will the job run?
medium
A. Every Monday at 9:00 AM
B. Every day at 9:00 AM
C. Every hour on the first day of the month
D. Every Monday at 12:00 AM

Solution

  1. Step 1: Decode the cron expression

    The expression means: minute=0, hour=9, day=any, month=any, day-of-week=1 (Monday).
  2. Step 2: Interpret the schedule

    This runs the job every Monday at 9:00 AM.
  3. Final Answer:

    Every Monday at 9:00 AM -> Option A
  4. Quick Check:

    0 9 * * 1 = Monday 9 AM [OK]
Hint: Day-of-week 1 means Monday in cron [OK]
Common Mistakes:
  • Thinking it runs daily
  • Confusing day-of-week with day-of-month
  • Mixing up hour and minute fields
4. You created a Cloud Scheduler job with cron expression 60 12 * * *. What is the issue?
medium
A. Minute value 60 is invalid; must be 0-59
B. Hour value 12 is invalid; must be 0-11
C. Day of month cannot be *
D. Cron expression is correct and will run daily at 12:00

Solution

  1. Step 1: Check minute field validity

    The minute field must be between 0 and 59. 60 is invalid.
  2. Step 2: Verify other fields

    Hour 12 is valid (represents noon), and day/month/day-of-week as * means every day.
  3. Final Answer:

    Minute value 60 is invalid; must be 0-59 -> Option A
  4. Quick Check:

    Minute must be 0-59, 60 is invalid [OK]
Hint: Minutes max at 59, 60 causes error [OK]
Common Mistakes:
  • Using 60 for minutes
  • Thinking hour 12 is invalid
  • Assuming * is not allowed for day
5. You want a Cloud Scheduler job to run every weekday at 7:30 AM and trigger a Cloud Function via HTTP. Which cron expression and target setup is correct?
hard
A. Cron: 7 30 * * 1-5, Target: Pub/Sub topic
B. Cron: 30 7 * * 1-5, Target: HTTP with Cloud Function URL
C. Cron: 30 7 * * 0,6, Target: HTTP with Cloud Function URL
D. Cron: 30 7 * * *, Target: Cloud Storage bucket

Solution

  1. Step 1: Choose correct cron expression for weekdays at 7:30 AM

    The expression 30 7 * * 1-5 means minute 30, hour 7, any day/month, weekdays (Monday=1 to Friday=5).
  2. Step 2: Select proper target for Cloud Function trigger

    Cloud Scheduler triggers Cloud Functions via HTTP target using the function's URL.
  3. Final Answer:

    Cron: 30 7 * * 1-5, Target: HTTP with Cloud Function URL -> Option B
  4. Quick Check:

    Weekdays 7:30 AM + HTTP trigger = Cron: 30 7 * * 1-5, Target: HTTP with Cloud Function URL [OK]
Hint: Weekdays are 1-5 in cron; Cloud Functions use HTTP triggers [OK]
Common Mistakes:
  • Swapping hour and minute
  • Using weekend days instead of weekdays
  • Wrong target type for Cloud Function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard