MSDN Documentation

Microsoft Developer Network

Azure Functions Timer Triggers

Last updated: September 28, 2023

Azure Functions provide a serverless compute experience that allows you to run small pieces of code, or "functions," in the cloud. Timer triggers are a common way to execute these functions on a schedule, enabling background tasks, batch processing, and recurring operations without manual intervention.

What are Timer Triggers?

A Timer trigger is a type of Azure Function trigger that executes your function on a specified schedule. This is achieved using a cron expression, which defines the recurrence pattern for the trigger.

Key Concepts

Cron Expressions

Timer triggers utilize a cron expression to define the schedule. A cron expression is a string consisting of six fields (or seven, if the seventh field, indicating seconds, is included):

* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └─ Day of the week (0 - 7) (Sunday=0 or 7)
│ │ │ │ └─ Month (1 - 12)
│ │ │ └─ Day of the month (1 - 31)
│ │ └─ Hour (0 - 23)
│ └─ Minute (0 - 59)
└─ Second (0 - 59)

Here are some common examples:

Function App Configuration

To use a timer trigger, you typically define it in your function's configuration file (e.g., function.json for JavaScript or Python, or using attributes in C#). The trigger configuration specifies the schedule and other optional parameters.

Example: function.json for JavaScript

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

This configuration will run the function every 5 minutes.

Example: C# Attributes

using Microsoft.Azure.WebJobs;

            public static class TimerTriggeredFunction
            {
                [FunctionName("MyTimerFunction")]
                public static void Run(
                    [TimerTrigger("0 */10 * * * *")] TimerInfo myTimer)
                {
                    // Your function logic here
                    System.Console.WriteLine($"C# Timer trigger function executed at: {System.DateTime.Now}");
                }
            }

This C# example runs the function every 10 minutes.

TimerInfo Object

When the timer trigger fires, the function receives a TimerInfo object (or its equivalent in your language) that provides information about the trigger instance, such as the schedule, the next due time, and whether it's a past due invocation.

Handling Past Due Invocations

By default, if your function app is down or experiences issues during a scheduled execution time, timer triggers are designed to be "at-least-once" reliable. This means that when the function app comes back online, it might execute missed invocations. You can control this behavior with the runOnStartup and useMonitor properties in your configuration.

Tip: For critical, time-sensitive operations, consider using Azure Logic Apps or Event Grid for more robust scheduling and guaranteed delivery mechanisms. Timer triggers are excellent for general-purpose scheduled tasks.

Common Use Cases

Best Practices

Important: For highly precise scheduling or complex workflows, consider using Azure Logic Apps or Durable Functions. Timer triggers are best suited for simpler, recurring tasks.

Next Steps