Scheduling Tasks With Cron for Python 2 Stay organized with collections Save and categorize content based on your preferences.
The App Engine Cron Service allows you to configure regularly scheduled tasksthat operate at defined times or regular intervals. These tasks are commonlyknown ascron jobs. These cron jobs are automatically triggered by theApp Engine Cron Service. For instance, you might use a cron job to sendout an email report on a daily basis, or to update some cached data every 10minutes, or refresh summary information once an hour.
A cron job makes a scheduled HTTPGET request to the specified endpoint in thesame app where the cron job is configured. The handler for that endpoint executesthe logic when it is called.
The App Engine Cron Service cannot be used to call web endpoints outside theApp Engine host app. It cannot be used to call App Engineendpoints from other apps besides the host app.
A cron job request is subject to the same limits as those forpush task queues.
Before you begin
To deploy or update schedules, your account requires one of the followingIAM roles:
- Owner
- Editor
You can set the permission on theIAM page in the Google Cloud console.
Creating a cron job
- Create the
cron.yamlfile in the root directory of yourapplication (alongsideapp.yaml). Add one or more
<cron>entries to your file and define thenecessary elements for your job, including the required<url>and<schedule>elements.Review the cron.yaml syntax andoptionsfor more details about the elements of thecron.yamlfile.The following example creates a basic cron job that runs daily:
cron:-description:"daily summary job"url:/tasks/summarytarget:betaschedule:every24hoursThe target specification is optional and is the name of a service/version.If present, the target is prepended to your app's hostname, causing the jobto be routed to that service/version.If no target is specified, the job will run in the versions of the
defaultservice that are configured for traffic.Create a handler for the cron job URL. The handler should execute any tasksthat you want scheduled. The handler should respond with an HTTP status codebetween 200 and 299 (inclusive) to indicate success. Other status codes canbe returned and can be used toretry the cron job.
Testing cron jobs in the development server
The local development server doesn't automatically run your cron jobs. You canmake requests directly to your cron job's URL to test your functionality. Youcan use your local cron or scheduled tasks interface to trigger the URLs of yourjobs withcurl or a similar tool.
You can use the admin interface of the local development server toview your cron jobs athttp://localhost:8000/cron.
Retrying cron jobs that fail
If a cron job's request handler returns a status code that is not in the range200–299 (inclusive) App Engine considers the job to have failed. By default,failed jobs are not retried unless a 503 status code is returned, in which caseit is retried every minute until it succeeds or returns a 200-299 status code.
To set failed jobs to be retried:
- Include a
retry_parametersblock in yourcron.yamlfile. Choose and set theretry parameters in the
retry_parametersblock.For example, this sample
cron.yamlfile contains a single cron job that isconfigured to retry up to five times (the default) with a starting backoffof 2.5 seconds that doubles each time.cron:-description:"retry demo"url:/retryschedule:every10minsretry_parameters:min_backoff_seconds:2.5max_doublings:5
Learn more about the cron retryoptions.
Deploying cron jobs
To deploy the cron jobs specified in yourcron.yaml configuration file,run the following command:
gcloudappdeploycron.yamlDeleting all cron jobs
To delete all cron jobs:
Edit the contents of the
cron.yamlfile to:cron:Deploy the
cron.yamlfile to App Engine.
Securing URLs for cron
A cron handler is just a normal handler defined inapp.yaml. You can preventusers from accessing URLs used by scheduled tasks by restricting access toadministrator accounts. Scheduled tasks can access admin-only URLs. You canrestrict a URL by addinglogin: admin to the handler configuration inapp.yaml.
An example might look like this inapp.yaml:
runtime:python27api_version:1handlers:-url:/report/weeklyscript:reports.applogin:adminlogin: admin, theycannot use URL paths restricted withlogin: required because cron scheduledtasks are not run as any user. Theadmin restriction is satisfied by theinclusion of theX-Appengine-Cron header described below.For more information see how to require login or admin status in theapp.yaml reference.To test a cron job, sign in as an administrator and visit the URL of the handlerin your browser.
Requests from the Cron Service will also contain a HTTP header:
X-Appengine-Cron:trueTheX-Appengine-Cron header is set internally by App Engine. If yourrequest handler finds this header it can trust that the request is a cronrequest. If the header is present in an external user request to your app, itis stripped, except for requests from logged in administrators of theapplication, who are allowed to set the header for testing purposes.
App Engine issues Cron requests from the IP address0.1.0.2. For Cron jobs created with older gcloud versions (earlier than326.0.0), Cron requests will come from0.1.0.1.
Calling Google Cloud Endpoints
You cannot specify aGoogle Cloud Endpoint intheurl field of a cron job.If you want your cron job to call a Google Cloud Endpoint,issue a request to a target that is served by a handler inyour app, and call the endpoint class and method from the handler code.
Viewing cron jobs in the Google Cloud console
You can view scheduled cron jobs inCloud Scheduler'sApp Engine Cron Jobstab.
You can alsoview logs to see when cron jobs were added or removed.
Learn more
See detailed information about defining cron jobs in thecron.yamlReference.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.