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.
The timer trigger is supported across all Azure Functions runtimes, including:
The timer trigger is configured in the function.json file for your function. The primary configuration setting is the schedule property.
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}
minute (0 - 59)hour (0 - 23)day (1 - 31)month (1 - 12)day-of-week (0 - 6, where 0 is Sunday, or use names like SUN, MON, TUE)year (Optional, typically ignored or not supported in Azure Functions)Special characters:
*: Matches any value.,: Specifies a list of values.-: Specifies a range of values./: Specifies interval.Example CRON expressions:
0 * * * * *: Run every minute.0 0 * * * *: Run every hour at the top of the hour.0 0 1 * * *: Run every day at midnight.0 0 0 * * 1: Run every Monday at midnight.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 * * * *"
}
]
}
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 * * * *"
}
]
}
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 * * * *"
}
]
}
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 * * * * *"
}
]
}
Here are examples of timer trigger configurations for different languages.
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 * * * *"
}
]
}
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 * * * *"
}
]
}
__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 * * * *"
}
]
}
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 * * * *"
}
]
}
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.
runOnStartup Behavior: Be cautious when using runOnStartup in production environments, as it can lead to unexpected behavior if not managed carefully.