Azure Functions Timer Trigger Bindings

Table of Contents

Introduction

The timer trigger allows you to run Azure Functions on a schedule. This is useful for tasks that need to be performed at regular intervals, such as batch processing, data synchronization, or sending scheduled reports.

The timer trigger uses a CRON expression to define the schedule. You can specify the interval in minutes, hours, days, weeks, or months.

Note: Timer triggers in Azure Functions are designed for tasks that are relatively short-lived. For long-running or complex scheduled operations, consider using other Azure services like Azure Logic Apps or Azure Batch.

Supported Languages

The timer trigger is supported across all Azure Functions runtimes, including:

Configuration

The timer trigger is configured in the function.json file for your function. The primary configuration setting is the schedule property.

Schedule Expression

The schedule property accepts a CRON expression. A CRON expression consists of six fields (or seven, depending on the system):

{minute} {hour} {day} {month} {day-of-week} {year}

Special characters:

Example CRON expressions:

Run on Startup

You can configure the timer trigger to run immediately when the function app starts by setting runOnStartup to true.

{
  "schedule": "0 */5 * * * *",
  "runOnStartup": true,
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

Use Monitor

The useMonitor property controls whether the timer trigger uses a monitoring mechanism to track successful executions and prevent repeated executions in case of restarts. The default is true.

{
  "schedule": "0 */5 * * * *",
  "runOnStartup": false,
  "useMonitor": true,
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

Schedule with ISO 8601

For simpler interval-based schedules, you can use ISO 8601 duration format.

Example: Run every 5 minutes.

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

Schedule with Time Span

You can also define intervals using a time span. This is often easier to read for common intervals.

Example: Run every 10 seconds.

{
  "schedule": "*/10 * * * * *",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "*/10 * * * * *"
    }
  ]
}
Note: The six-field CRON syntax (including seconds) is typically used for timer triggers. If you specify a five-field CRON expression, the seconds field defaults to 0.

Examples

Here are examples of timer trigger configurations for different languages.

C#

MyTimerTrigger.cs

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

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

function.json

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

JavaScript

index.js

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);
};

function.json

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

Python

__init__.py

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 executed at %s', utc_timestamp)

function.json

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

PowerShell

run.ps1

param($myTimer)

$now = Get-Date
$TimeStamp = $now.ToString("yyyy-MM-ddTHH:mm:ssZ")

if ($myTimer.isPastDue) {
    Write-Host "PowerShell timer trigger function ran at $TimeStamp"
    Write-Host "The timer is past due!"
}
else {
    Write-Host "PowerShell timer trigger function ran at $TimeStamp"
}

function.json

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

Advanced Scenarios

The timer trigger can be combined with other bindings to create sophisticated scheduled workflows. For example, you can use an output binding to write data to a storage account or a queue binding to trigger another function.

Distributed Locking: In scenarios with multiple instances of your function app, you might need to ensure that a scheduled task runs only once across all instances. Azure Functions provides a built-in mechanism for this when useMonitor is true. The timer trigger uses blob storage to coordinate and prevent duplicate executions.

Troubleshooting