0
0
AzureHow-ToBeginner · 4 min read

How to Use Timer Trigger Azure Function: Simple Guide

To use a timer trigger in an Azure Function, define a function with a timer schedule using a CRON expression in the function.json or attribute. This trigger runs your code automatically on the schedule you set, like every 5 minutes or daily at midnight.
📐

Syntax

The timer trigger uses a CRON expression to schedule when the function runs. You specify this schedule in the function.json file or as an attribute in your code. The key parts are:

  • schedule: A CRON expression defining the run times.
  • runOnStartup (optional): Runs the function immediately on startup if true.
  • useMonitor (optional): Enables monitoring for missed runs.
json
{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *",
      "runOnStartup": true
    }
  ]
}
💻

Example

This example shows a C# Azure Function that runs every 5 minutes and logs the current time. It uses the timer trigger with a CRON schedule.

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

public static class TimerFunction
{
    [FunctionName("TimerFunction")]
    public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"Timer trigger function executed at: {DateTime.Now}");
    }
}
Output
Timer trigger function executed at: 2024-06-01 12:00:00 Timer trigger function executed at: 2024-06-01 12:05:00 ...
⚠️

Common Pitfalls

Common mistakes when using timer triggers include:

  • Using incorrect CRON expressions that do not match the expected 6-field format.
  • Not setting RunOnStartup when you want the function to run immediately after deployment.
  • Ignoring time zone differences; Azure Functions use UTC time by default.
  • Not handling exceptions inside the function, which can cause silent failures.
json
/* Wrong CRON (5 fields instead of 6) */
"schedule": "*/5 * * * *"

/* Correct CRON (6 fields) */
"schedule": "0 */5 * * * *"
📊

Quick Reference

Timer trigger key points:

  • Schedule uses 6-field CRON: second minute hour day month day-of-week
  • Default time zone is UTC
  • Use RunOnStartup=true to run immediately after deployment
  • Function runs automatically on schedule without manual calls

Key Takeaways

Use a 6-field CRON expression to schedule your timer trigger function.
Azure Functions timer triggers run automatically on the schedule you set.
Remember Azure Functions use UTC time by default for scheduling.
Set RunOnStartup=true to run the function immediately after deployment.
Handle exceptions inside your function to avoid silent failures.