---
title: "How to Schedule (Cron) Jobs with Cloud Functions for Firebase"
description: "How to Schedule (Cron) Jobs with Cloud Functions for Firebase"
published: "2017-03-24"
last_updated: "2017-03-24"
authors:
  - "Abe Haskins (Developer Programs Engineer)"
categories:
  - "Cloud Functions"
canonical_url: "https://firebase.blog/posts/2017/03/how-to-schedule-cron-jobs-with-cloud"
---

# How to Schedule (Cron) Jobs with Cloud Functions for Firebase

*Published March 24, 2017 by Abe Haskins (Developer Programs Engineer)*

---
#### Update!
Cloud Functions for Firebase now supports [native scheduling](https://firebase.google.com/docs/functions/schedule-functions)!

```js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This will be run every 5 minutes!');
  return null;
});
```

The article below predates this feature and is useful if you want to use AppEngine. However, we recommend using the scheduled functions feature provided by Cloud Functions.

* * *

Cloud Functions are a great solution for running backend code for your Firebase app. You can write a function which is triggered by many different actions like user sign-ups, writes to the Realtime Database, changes to a Cloud Storage bucket, or conversion events in Firebase Analytics. Cloud Functions can also be triggered by some external sources, for example you could tie a Cloud Function to an HTTPS endpoint or a [Cloud Pub/Sub](https://cloud.google.com/pubsub/) topic.  

Reacting to these events is very powerful, but you may not always want to _react_ to an event - sometimes you may want to run a function based on a time interval. For example, you could [clean up extra data](https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron) in your Realtime Database every night, or run analysis on your Analytics data every hour. If you have a task like this, you'll want to use App Engine Cron with Cloud Functions for Firebase to reliably trigger a function at a regular interval.

### How to Schedule Functions with AppEngine

![firebase_floatright_appengine](https://firebasestorage.googleapis.com/v0/b/first-class-blog.appspot.com/o/blog-assets%2F2017%2F03%2Fhow-to-schedule-cron-jobs-with-cloud%2Foptimized%2Ffirebase_floatright_appengine.jpg?alt=media)

Cloud Functions for Firebase does not have any special support which allow us to utilize App Engine Cron to schedule events. In fact, the solution we'll implement is nearly identical to the solution we recommend for doing [reliable task scheduling on Google Compute Engine](https://cloud.google.com/solutions/reliable-task-scheduling-compute-engine).  

The trick is to create a tiny App Engine shim that provides hooks for App Engine Cron. These hooks will then push to Cloud Pub/Sub topics for each scheduled job.  

We will then configure our Cloud Function to handle incoming messages on that Pub/Sub topic.  
Although this solution is the preferred option for scheduling functions, it isn't the _only_ way you achieve this goal. If you're interested in seeing an alternative method, you should check out the [functions-samples](https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron) repository which explains how to achieve a similar result using an external scheduling service.  

### Deploying the App Engine App 
It just so happens that we've already written the App Engine app you'll need to set up scheduled functions. It's available in the [firebase/functions-cron](https://github.com/firebase/functions-cron) repo on Github.  

By default this sample triggers hourly, daily, and weekly Cloud Pub/Sub ticks. If you want to customize this schedule for your app then you can modify the [cron.yaml](https://github.com/firebase/functions-cron/blob/master/appengine/cron.yaml).  

For details on configuring this, please see the [cron.yaml Reference](https://cloud.google.com/appengine/docs/standard/python/config/cronref) in the App Engine documentation.  

Let's get started!

### 1. Prerequisites

Install (or check that you have previously installed) the following tools.  

*   [git](https://git-scm.com/downloads)
*   [Python 2.7](https://www.python.org/download/releases/2.7/)
*   [Python pip](https://pip.pypa.io/en/latest/installing.html)
*   [Google Cloud SDK](http://cloud.google.com/sdk/)

### 2. Clone this repository

To clone the GitHub repository to your computer, run the following command:  

```js
git clone https://github.com/firebase/functions-cron 
```

Change directories to the functions-cron directory. The exact path depends on where you placed the directory when you cloned the sample files from GitHub.  

```js
cd functions-cron 
```

### 3. Deploy to App Engine

Configure the gcloud command-line tool to use your Firebase project.  

```js
gcloud config set project  
```

Change directory to `appengine/`  

```js
cd appengine/ 
```

Install the Python dependencies

```js
$ pip install -t lib -r requirements.txt 
```

Create an App Engine App  

```js
gcloud app create 
```

Deploy the application to App Engine.

```js
gcloud app deploy app.yaml  cron.yaml 
```

Open [Google Cloud Logging](https://console.cloud.google.com/logs/viewer) and in the right dropdown select "GAE Application". If you don't see this option, it may mean that App Engine is still in the process of deploying.  

Look for a log entry calling `/_ah/start`. If this entry isn't an error, then you're done deploying the App Engine app.  

### 4. Deploy to Google Cloud Functions for Firebase

Ensure you're back the root of the repository (`cd ..` if you're coming from Step 2)  

Deploy the sample `hourly_job` function to Google Cloud Functions

```js
firebase deploy --only functions --project  
```

**Warning:** This will remove any existing functions you have deployed. If you have existing functions, copy the example from [functions/index.js](https://github.com/firebase/functions-cron/blob/master/functions/index.js) into your project's `index.js`

### 5. Verify your Cron Jobs

We can verify that our function is wired up correctly by opening the [Task Queue](https://console.cloud.google.com/appengine/taskqueues) tab in App Engine and clicking on Cron Jobs. Each of these jobs has a Run Now button next to it.  

The sample functions we deployed only has one function: `hourly_job`. To trigger this job, let's hit the Run Now button for the `/publish/hourly-tick` job.  

Then, go to your terminal and run...  

```js
firebase functions:log --project  
```

You should see a successful `console.log` from your `hourly_job`.  
### You're Done! 
Your cron jobs will now "tick" along forever. As we mentioned above, you're not limited to the `hourly-tick`, `daily-tick` and `weekly-tick` that are included in the App Engine app.  

You can add more scheduled functions by modifying the [cron.yaml](https://github.com/firebase/functions-cron/blob/master/appengine/cron.yaml) file and re-deploying the app.
