The Timer trigger allows you to run a Function at regular intervals. This is useful for background tasks, scheduled jobs, or periodic data processing.

Timer Trigger Configuration

The Timer trigger is configured using a JSON object in your function.json file. The primary property is schedule, which accepts a CRON expression.

CRON Expressions

CRON expressions are strings that define a schedule. They consist of six fields:

Field Description Allowed Values Required
Minute The minute of the hour. 0-59 Yes
Hour The hour of the day. 0-23 Yes
Day of Month The day of the month. 1-31 Yes
Month The month of the year. 1-12 Yes
Day of Week The day of the week. 0-6 (0=Sunday, 1=Monday, ..., 6=Saturday) Yes
Year The year. 1970-2099 Yes

Special characters include:

Example CRON Expressions

Function.json Configuration

Here's an example of how to configure a Timer trigger in your function.json:


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

This configuration will trigger the function every 5 minutes.

Timer Information

When the Timer trigger fires, it passes a TimerInfo object to your function. This object contains information about the trigger, such as:

Important: The Timer trigger uses UTC for all scheduling. Ensure your CRON expressions are set according to UTC.

Programming Language Examples

Python


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)
    logging.info('Next timer schedule is: %s', mytimer.next)
            

C#


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

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

            if (myTimer.IsPastDue)
            {
                log.LogInformation("Timer is past due!");
            }

            log.LogInformation($"Next timer schedule: {myTimer.ScheduleStatus.Next}");
        }
    }
}
            

JavaScript


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

    if (myTimer.isPastDue) {
        context.log('JavaScript timer trigger function is past due!');
    }
    context.log('JavaScript timer trigger function executed at!', timeStamp);
    context.log('Next timer schedule is: ', myTimer.next);
};
            

Timeouts and Delays

The Timer trigger is designed for running tasks at intervals, not for long-running operations within a single invocation. If your task takes longer than the function app's timeout, it may be terminated. For long-running tasks, consider using other Azure services like Azure Logic Apps or Azure Batch.

Troubleshooting