Azure Functions Timer Trigger Reference

This document provides detailed information about configuring and using the Timer trigger for Azure Functions.

Overview

The Timer trigger allows you to run Azure Functions on a schedule. It's ideal for tasks that need to be executed periodically, such as:

The Timer trigger uses a schedule defined by a CRON expression, providing flexible control over execution times.

Configuration

The Timer trigger is configured via the function.json file for your function. The primary configuration setting is the schedule property, which accepts a CRON expression.

function.json Example

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */10 * * * *"
    }
  ]
}

Configuration Properties

Property Description Required Type Default
name The name of the trigger. This name is used in your function code to access the trigger metadata. Yes String N/A
type Must be set to timerTrigger. Yes String N/A
direction Must be set to in for triggers. Yes String N/A
schedule The CRON expression that defines when the function should run. This can also be a string or a special binding expression like %MySetting% to read from an app setting. Yes String N/A
runOnStartup If set to true, the function will run once when the function app starts up. This is useful for initialization tasks. No Boolean false
useMonitor When set to true, the timer trigger is hosted on a separate worker process. This is the default in Consumption and Premium plans for longer-running timers and provides better reliability. No Boolean true (in Consumption and Premium plans)

CRON Expressions

CRON expressions are a powerful way to define schedules. Azure Functions uses a 6-field CRON format:

field        allowed values    allowed characters
----------   --------------    ------------------
Minute        0-59             * , - /
Hour          0-23             * , - /
Day of month  1-31             * , - / L W
Month         1-12             * , - /
Day of week   0-6 (0=Sunday)   * , - / L
Year          1970–2199        * , - /

Special Characters:

CRON Expression Examples:

CRON Expression Description
0 */10 * * * * Every 10 minutes.
0 0 12 * * Mon-Fri Every day at 12:00 PM (noon) from Monday to Friday.
0 0 0 1 * * At midnight on the 1st of every month.
0 0 L * * * At midnight on the last day of every month.
0 0 8-18 * * Mon-Fri Every hour between 8 AM and 6 PM on weekdays.

Note: The timer trigger is configured to run on a schedule based on UTC time. Ensure your CRON expressions account for the appropriate time zone conversion if needed.

Examples

Running Every Hour

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 * * * *"
    }
  ]
}

Running Every 5 Minutes

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "*/5 * * * * *"
    }
  ]
}

Running Daily at 9 AM UTC

{
  "scriptFile": "Program.cs",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 9 * * *"
    }
  ]
}

Running on Startup and Every Day at Midnight

{
  "scriptFile": "MyTimerFunction.ts",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 0 * * *",
      "runOnStartup": true
    }
  ]
}

Usage in Function Code

When the timer trigger fires, the trigger metadata is passed to your function. The exact type of this metadata depends on the language and runtime you are using.

C# Example

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class TimerTriggerCSharp
{
    [FunctionName("TimerTriggerCSharp")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");
    }
}

JavaScript Example

module.exports = async function (context, myTimer) {
    const timeStamp = new Date().toISOString();

    if (myTimer.isPastDue)
    {
        context.log('JavaScript isPastDue!');
    }
    context.log('JavaScript timer trigger function executed!', timeStamp);
};

Python Example

import datetime
import logging

import azure.functions as func

def main(myTimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().isoformat()

    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function is running at %s', utc_timestamp)

Important: The TimerInfo object (or its equivalent in other languages) can provide information about the schedule and whether the execution is past due. Use this information judiciously to handle potential delays or missed executions.